user880386
user880386

Reputation: 2827

AngularJs : Variable from directive to controller

How can pass the variable itemSelect inside my directive to my controller?

mDirective.directive('directive', function() {
    return {
        restrict: 'A',
        scope: {
            options: "="
        },
        templateUrl: '',

        link: function(scope, element, attrs) {  
                .........               
             $(element).find('.typeY').on('change', function() {
                var itemSelect =   $(element).find('.typeY').val();
            });
        } ,

    };
});

Upvotes: 0

Views: 57

Answers (1)

Chandermani
Chandermani

Reputation: 42669

Something like

mDirective.directive('directive', function() {
    return {
        restrict: 'A',
        scope: {
            options: "=",
            selected:"=",
        },
        templateUrl: '',

        link: function(scope, element, attrs) {  
                .........               
            $(element).find('.typeY').on('change', function() {
                scope.$apply(function() {
                  scope.selected=value;  // value from the element
                });
            });
        } ,

    };
});

At html level

<div directive options='expression' selected='expressionToTheScopeProperty'/>

Upvotes: 1

Related Questions