AngularJs:从指令到控制器的变量

时间:2014-01-20 15:42:04

标签: angularjs angularjs-directive angularjs-scope

如何将我的指令中的变量itemSelect传递给我的控制器?

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();
            });
        } ,

    };
});

1 个答案:

答案 0 :(得分:1)

这样的东西
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
                });
            });
        } ,

    };
});

在html级别

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