angularjs 指令scope中“=”、“@”、“&”的区别

HTML

<div ng-controller="MyCtrl">
    <h2>Parent Scope</h2>
    <input ng-model="foo"> <i>// Update to see how parent scope interacts with component scope</i>    
    <br><br>
    <!-- attribute-foo binds to a DOM attribute which is always
    a string. That is why we are wrapping it in curly braces so 
    that it can be interpolated. 
    -->
    <my-component attribute-string="bindAString" attribute-foo="{{foo}}" binding-foo="foo" 
        isolated-expression-foo="updateFoo(newFoo)" >
        <h2>Attribute</h2>
        <div>
            <strong>get:</strong> {{isolatedAttributeFoo}}
        </div>
        <div>
            <strong>set:</strong> <input ng-model="isolatedAttributeFoo">
            <i>// This does not update the parent scope.</i>
            <br>
            <strong>set:</strong> <input ng-model="isolatedBindingString">
            <i>// This does not update the parent scope.</i>
        </div>
        <h2>Binding</h2>
        <div>
            <strong>get:</strong> {{isolatedBindingFoo}}
        </div>
        <div>
            <strong>set:</strong> <input ng-model="isolatedBindingFoo">
            <i>// This does update the parent scope.</i>
            
        </div>
        <h2>Expression</h2>    
        <div>
            <input ng-model="isolatedFoo">
            <button class="btn" ng-click="isolatedExpressionFoo({newFoo:isolatedFoo})">Submit</button>
            <i>// And this calls a function on the parent scope.</i>
        </div>
    </my-component> </div>

JS

var myModule = angular.module('myModule', [])
    .directive('myComponent', function () {
        return {
            restrict:'E',
            scope:{
                /* NOTE: Normally I would set my attributes and bindings
                to be the same name but I wanted to delineate between 
                parent and isolated scope. */ 
                isolatedBindingString:'@attributeString',
                isolatedAttributeFoo:'@attributeFoo',
                isolatedBindingFoo:'=bindingFoo',
                isolatedExpressionFoo:'&'
            }        
        };
    })
    .controller('MyCtrl', ['$scope', function ($scope) {
        $scope.foo = 'Hello!';
        $scope.updateFoo = function (newFoo) {
            $scope.foo = newFoo;
        }
    }]);

clipboard.png

代码和效果地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@符号用于在directive的scope传递字符串类型的参数,而&符号用于在directive的scope传递一个函数类型的参数。 当directive需要调用父作用域的一个函数时,可以使用&符号传递该函数。使用&符号时,指令使用的属性名必须与父作用域函数名相同,并且在调用函数时,需要使用一个对象来传递参数。 例如,我们可以在HTML这样使用directive来传递一个函数参数: ``` <my-directive my-cancel="cancel()"></my-directive> ``` 在directive的定义,我们可以使用&符号将my-cancel的值传递给directive的scope的一个属性: ``` app.directive('myDirective', function() { return { scope: { myCancel: '&' }, link: function(scope, element, attrs) { scope.cancel = function() { console.log('cancel function called'); }; element.on('click', function() { scope.myCancel(); }); } }; }); ``` 在上面的例子,我们将my-cancel的值传递给directive的scope的myCancel属性,并且在link函数通过scope.myCancel()来调用该函数。当点击指令的元素时,会调用父作用域的cancel函数,并输出'cancel function called'。 需要注意的是,在调用函数时,需要使用一个对象来传递参数。例如,如果我们需要将一个字符串参数传递给cancel函数,可以在HTML这样使用directive: ``` <my-directive my-cancel="cancel(someString)"></my-directive> ``` 然后,在调用函数时,需要传递一个包含参数的对象: ``` scope.myCancel({someString: 'foo'}); ``` 这样,父作用域的cancel函数就会接收到'someString'参数,并输出'cancel function called with parameter: foo'。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值