0

I'm sorry if it a duplicate of someone question, however I can't find solution to my problem.

I want to make incapsulated component and provide data throw a function to main controller but all my argumnts are undefined. Seems I did something wrong.

Here is code

component:

(function () {

    'use strict';

    angular
        .module('app')
        .component('datePicker', {
            template: createTemplate(),
            controllerAs: 'vm',
            bindings: {
                    onSelectDate: '&'
            },
            controller: DatepickerController
        });

        function DatepickerController() {
            var vm = this;
            vm.filterDateFrom = new Date();
            vm.filterDateTo = new Date();

            vm.submitDate = submitDate;

            function submitDate() {
                console.log(vm.filterDateFrom); // here I got dates from and to
                console.log(vm.filterDateTo);
                vm.onSelectDate(vm.filterDateFrom, vm.filterDateTo)
            }
        }
    }

})();

main template

<date-picker
      on-select-date="ctrl.submitFilter(from, to)">
</date-picker>

main controller

function submitFilter(from, to) {
    console.log(from, to) // here all arguments are undefined
}

1 Answer 1

1

Use a locals object:

app.component('datePicker', {
    template: createTemplate(),
    controllerAs: 'vm',
    bindings: {
            onSelectDate: '&'
    },
    controller: "DatepickerController"
});

app.controller("DatepickerController", function() {
    var vm = this;
    vm.filterDateFrom = new Date();
    vm.filterDateTo = new Date();

    vm.submitDate = submitDate;

    function submitDate() {
        console.log(vm.filterDateFrom); // here I got dates from and to
        console.log(vm.filterDateTo);
        ̶v̶m̶.̶o̶n̶S̶e̶l̶e̶c̶t̶D̶a̶t̶e̶(̶v̶m̶.̶f̶i̶l̶t̶e̶r̶D̶a̶t̶e̶F̶r̶o̶m̶,̶ ̶v̶m̶.̶f̶i̶l̶t̶e̶r̶D̶a̶t̶e̶T̶o̶)̶ 
        vm.onSelectDate({
           "from": vm.filterDateFrom,
           "to":   vm.filterDateTo
        });
    }
});

To pass data from the isolated scope via an expression to the parent scope, pass a map of local variable names and values into the expression wrapper fn.

For more information, see

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.