【问题标题】:When to use AngularJS's one time binding (i.e. `<`) introduced in 1.5?何时使用 AngularJS 1.5 中引入的一次性绑定(即`<`)?
【发布时间】:2017-01-28 22:28:33
【问题描述】:

给定

<my-component my-attr="parentModel">

和一个指令定义,包括:

scope: { localModel:'<myAttr' }

角度will set up a one time binding。这是什么意思

表达式在父作用域的上下文中计算

隔离范围属性 localModel 将在父范围上反映 parentModel 的值。对 parentModel 的任何更改都会反映在 localModel 中,但 localModel 的更改不会反映在 parentModel 中

这很好,但是在使用 & 符号(即&amp;)的角度表达式已经可以完成的事情之上,如何完成任何事情?

给定

<my-component my-attr="parentModel">

和一个指令定义,包括:

scope: { getModel:'&myAttr' }

scope.getModel() 的任何调用也应该在父作用域的上下文中评估"parentModel" 表达式,并将这样的值提供给指令的隔离作用域,这里不需要$watch parentModel或者担心隔离范围内的值会传播回父级。

【问题讨论】:

  • 为什么不@ vs &amp;@ vs &lt; 呢? &amp;&lt; 是橙子和苹果。 &amp; 计算父上下文中的表达式。 &lt; 提供范围属性的绑定。 &amp; 非常适合在父上下文中调用函数(A2 事件发射器的替代品),scope.localModel() 非常难闻。 &lt; 首先是为了填补 1.5 到 2.0 迁移的空白。
  • @estus 你不会叫它scope.localModel()。只是保留名字以供比较。更像是scope.someBehavior()
  • @estus 我想我的问题是,当您可以使用函数完成相同的事情时,为什么要将道具绑定到从父级接收一个定向更新的作用域。它可能只是归结为一种风格/偏好吗?
  • 你可以用{{ parentModel }}@绑定来做到这一点,不是吗?这在某种程度上是一个品味问题,但使用 & 绑定会更尴尬。
  • 如果绑定不正确,[] 和 () 绑定。

标签: javascript angularjs


【解决方案1】:

我编写了这段代码 sn-p 试图更好地理解这个问题。这两个选项之间似乎有明显的区别,例如link 函数的处理方式和一些自动$watch()ing。但我从来没有以这种方式使用过表达式,我认为我遗漏了一些东西。

var app = angular.module('app', []);

app.controller('MainCtrl', function() {
  this.v1 = 1;
  this.v2 = 2;
  this.v3 = 3;
});

app.directive('myComponentOne',
  function() {
    return {
      restrict: 'E',
      scope: { v: "<val" },
      template: '<input ng-model="v"/>',
      link: s => s.v = 99
    };
  });

app.directive('myComponentTwo',
  function() {
    return {
      restrict: 'E',
      scope: { v: '&val' },
      template: '<input ng-model="v"/>',
      link: s => s.v = 99
    };
  });

app.directive('myComponentThree',
  function() {
    return {
      restrict: 'E',
      scope: { v: '=val' },
      template: '<input ng-model="v"/>',
      link: s => s.v = 99
    };
  });
<div ng-app="app">
  <div ng-controller="MainCtrl as main">
    <div>
      One-way binding:<br>
      parentScope=<input ng-model="main.v1" /><br>
      localScope=<my-component-one val="main.v1"></my-component-one>
    </div>
    <hr>
    <div>
      Expression:<br>
      parentScope=<input ng-model="main.v2" /><br>
      localScope=<my-component-two val="main.v2"></my-component-two>
    </div>
    <hr>
    <div>
      Two-way binding:<br>
      parentScope=<input ng-model="main.v3" /><br>
      localScope=<my-component-three val="main.v3"></my-component-three>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.1/angular.min.js"></script>

【讨论】:

  • 感谢您的回答/sn-p。尽管 b/c component-two 没有正确使用表达式,但这并不能完全解决问题。在 linkFn 中设置 s.v = 99 会覆盖包装表达式。即使您按原样保留 exp,&lt;input ng-model="v"/&gt; 中的 v 现在指向一个函数。添加ng-model-options="{getterSetter: true}" 也无济于事,在这种情况下,包装的 exp 只是一个吸气剂。一个更好的测试可能类似于{{v()}}&amp; 的情况下{{v}}&lt; 的情况下。您可以在后一种情况下设置一个 ng-click="v=10" 来测试它是一种方式
猜你喜欢
  • 2017-05-17
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2016-12-24
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多