为角范围结合及(符号)一次性绑定? [英] Is the angular scope binding &(ampersand) a one time binding?

查看:137
本文介绍了为角范围结合及(符号)一次性绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的角度范围,结合及(符号)一次性绑定?
我把它称为一个单向绑定,但它也一次性的?

Is the angular scope binding &(ampersand) a one time binding? I see it referred to as a one-way binding, but is it also one-time?

让我们说我有:

<my-custom-directive data-item="item" />

和我的指令声明如下:

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '/template.html',
    scope: {
        dataItem: '&'
    }
    controller: function ($scope) {
        // ....
    }
}])

我问如果绑定的原因是一次性的,因为这似乎是什么,我观察,那是。如果项目父范围更新,一个在该指令不会被更新。

The reason I'm asking if the binding is one-time is because that seems to be what I'm observing, that is. If item in the parent scope is updated, the one in the directive is not updated.

我是说得很对,绑定是一次?

要达到我想要的,这里的指令保存副本,而不影响父范围的项目 - 我这样做:

To achieve what I want, where the directive keeps a copy without affecting the parent scope's item -- I did this:

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '/template.html',
    scope: {
        dataItemOriginal: '='
    },
    link: function ($scope) {
        $scope.$watch('dataItemOriginal', function () {
            $scope.dataItem = window.angular.copy($scope.dataItemOriginal);
        });
    },
    controller: function ($scope) {
   //....
   }
}])

这是正确的或者是有一个更好的办法?

Is this proper or is there a better way?

推荐答案

有一个更好的办法。您当前的解决方案是建立三款腕表 - 两个是通过创建使用=绑定,然后额外的$看你创建进行复印。 $手表是比较昂贵的。

There is a better way. Your current solution is setting up three watches - two are created by using the "=" binding, and then the extra $watch you create to make a copy. $watches are relatively expensive.

下面是一个不创造任何手表一种替代方案:

Here is an alternative that doesn't create any watches:

.directive('myCustomDirective', [
'$log', function ($log) {
return {
    restrict: 'E',
    templateUrl: '<div ng-click="clicked()">Click me for current value</div>',
    scope: {
        item: '&'
    },
    controller: function($scope) {
        $scope.clicked = function(){
            alert(item());  //item() returns current value of parent's $scope.item property
        }
        $scope.val = item();  //val is the initial value of $parent.item
        $scope.val = 42; //$parent.item is unaffected. 
    }

}])

&安培;是高度误解。虽然它可以用于传递功能集成到一个孤立的范围,它实际上做的是:

& is highly misunderstood. While it can be used for passing functions into an isolated scope, what it actually does is:

提供了一种在父的上下文中执行一个前pression
  范围

provides a way to execute an expression in the context of the parent scope

它生成的指令,调用它时,执行父范围的背景下,前pression功能。恩pression不必是一个函数或函数调用。它可以是任何有效的角度EX pression。

It generates a function in the directive that, when called, executes the expression in the context of the parent scope. The expression does not have to be a function or function call. It can be any valid angular expression.

因此​​,在你的例子:

So in your example:

<my-custom-directive data-item="item"></my-custom-directive>

scope: {
    item: '&'
}

在指令

$ scope.item将是您可以在控制器或在您的模板调用一个函数。调用该函数将返回项,在您的父母范围内引用的对象。没有与&放无约束力。 - 不腕表均采用

$scope.item in the directive will be a function that you can call in the controller or in your template. Invoking that function will return the object referenced by "item" in your parent scope. There is no binding with & - no watches are used.

当单向结合名不副实进来是,使用与放大器时,该指令不能使用时,=,该指令改变$ parent.item,那里的价值,凭借$手表创建的,可以。它也不是一次性,或者说,因为它不是在技术上所有绑定。

Where the "one-way-binding" misnomer comes in is that using &, the directive cannot change the value of $parent.item, where as when using "=", the directive, by virtue of the $watches created, can. It is also not "one time", either, since it's not technically bound at all.

由于该角用于生成功能的机制涉及$解析,它是可能的指令中的本地人与来自指令值前pression的覆盖值传递。因此,在该指令的控制器,如果你这样做:

Since the mechanism that angular uses to generate the function involves $parse, it is possible for the directive to pass in "locals" that override values in the expression with values from the directive. So, in the directive controller, if you did:

item({item: 42})

,将始终返回42,不管项目中的父范围的值的。正是这种特性,使得&放大器;从指令与数据父范围执行的函数前pressions有用的。

it would always return 42, regardless of the value of item in the parent scope. It is this feature that makes & useful for executing function expressions on the parent scope with data from the directive.

这篇关于为角范围结合及(符号)一次性绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆