首页 > angularjs中,编写directive,scope中的“=”是什么意思?该于何时使用?

angularjs中,编写directive,scope中的“=”是什么意思?该于何时使用?

html<input my-datepicker name="test" callback="vm.callback()"/>
javascriptmodule.directive('myDatepicker', function(){
    return {
        scope : {
            callback : '&'
        },
        link : function( scope, el ){
            el.datepicker({
                change : function(){
                    scope.callback();
                }
            });
        },
    };
});
module.controller('myCtrl', function(){
    vm.callback = function(){
        //一些代码
    }
});

我知道通过指定scope中的绑定策略为&,可以实现调用controller中的定义的回调函数。

我的问题是,何时该使用=编写指令?有何效果?啊,跪求大神解答~~


这涉及到自定义指令的时候,如果你创建了单独的scope,如何“继承”来自父scope的数据问题。
符号定义有三种,@, =, 和 &,分别代表的意思是:把当前属性作为字符串传递 , 绑定当前属性,它带有一个来自指令父scope的属性,如同楼上说的那样 , 传递一个来自父scope的函数,可以稍后调用,调用的时候可以传递数据,例如scope.callback({a:'xxx',b:'yyy'})


=是表达式关联,我们还是从一个简单的例子来看问题:

    <div ng-controller="DemoCtrl">
        <demo-dir opts="options"></demo-dir>
    </div>
    module.controller('DemoCtrl', function($scope){
        $scope.options = {
            title: 'aaa',
            content: 'xxx'
        };
    });

    module.directive('demoDir', function(){
        return {
            restrict: 'E',
            scope: {
                opts: '='
            },
            link: function($scope, element, attr){
                console.log($scope.opts);  //这里输出的其实就是上述DemoCtrl里定义的options
            }
        }
    });
【热门文章】
【热门文章】