简体   繁体   English

Angular 1.5+组件可选单向绑定

[英]Angular 1.5+ component optional one-way binding

Taken from the AngularJS 1 documentation : 取自AngularJS 1 文档

You can also make the binding optional by adding ? 您还可以通过添加来使绑定可选? : <? <? or <?attr . <?attr

How does the optional one differ from the non-optional one for the one-way binding? 对于单向绑定,可选项与非可选项有何不同?

I can seem to figure out the differences for the optional version of two-way ( = ) and delegate ( & ) bindings here on my fiddle: https://jsfiddle.net/glenn/ze2wo0s1/ , but not for the one-way one. 我似乎可以在我的小提琴上找出可选版本的双向( = )和委托( & )绑定的差异: https//jsfiddle.net/glenn/ze2wo0s1/ ,但不是单向的一。

By the way, a very Merry Christmas! 顺便说一下,圣诞快乐! 🎅🏻🎄❤️ 🎅🏻🎄❤️

You can see the how it's handled in the source code: https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L3523 . 您可以在源代码中看到它的处理方式: https//github.com/angular/angular.js/blob/master/src/ng/compile.js#L3523

To me, it looks like if you use <? 对我来说,如果你使用<? and make the binding optional, it breaks early without setting up a watch. 并使绑定可选,它提前打破而不设置手表。 If use use < and make it required, it sets the binding to undefined and sets up a watch. 如果使用<并使其成为必需,它会将绑定设置为undefined并设置监视。 However, it appears to be just watching undefined , so in practice, there's no difference at all except for that one call to recordChanges . 但是,它看起来只是看undefined ,所以在实践中,除了对recordChanges一次调用之外没有任何区别。 In the case that you omit a required binding, the binding that's required will be a key in the changes object that is passed to $onChanges hook on the first call. 在省略所需绑定的情况下,所需的绑定将是在第一次调用时传递给$onChanges挂钩的changes对象中的键。 However, when you omit an optional binding, it will not be a key in the changes object. 但是,当您省略可选绑定时,它将不是changes对象中的键。

For an example see this JSFiddle . 有关示例,请参阅此JSFiddle requiredBinding and optionalBinding are both omitted, and thus, initialized to undefined , but requiredBinding is a key on the change object, whereas optionalBinding is not. requiredBindingoptionalBinding都被省略,因此,初始化为undefined ,但requiredBindingchange对象上的键,而optionalBinding则不是。

Using <? 使用<? makes it possible for the controller to change the value of the variable that was supposed to be bound, only if that variable is not present. 只有当该变量不存在时,控制器才能更改应该绑定的变量的值。

The optional bindings can be modified in the controller when they are NOT present. 当控制器不存在时,可以在控制器中修改它们。 If the value is passed to the component, then there's no way of changing it. 如果值传递给组件,则无法更改它。

The non-optional bindings can not be modified whatsoever. 无法修改非可选绑定。 If they are not present, they are undefined and they can not be modified at all. 如果它们不存在,则它们是undefined ,根本不能修改它们。

for example, assume you have this: 例如,假设你有这个:

bindings: {
  nameOptional: '<?',
  nameRequired: '<'
}

In the controller, you can not simply do $ctrl.nameRequired = 'something else' and expect the view to get updated. 在控制器中,您不能简单地执行$ctrl.nameRequired = 'something else'并期望视图得到更新。 However, you can do the same with nameOptional with one condition: only if name-optional is not passed to the component. 但是,您可以使用一个条件对nameOptional执行相同nameOptional :仅当name-optional未传递给组件时。 Only then the variable is the controller's to change. 只有这时变量才是要改变的控制器。

For better understanding you can refer this fiddle . 为了更好地理解你可以参考这个小提琴

Note that to simplify the thing, we're using a string which is passed by value. 请注意,为简化事物,我们使用的是一个按值传递的字符串。 If you are passing objects, the object's properties can always get modified in normal conditions. 如果要传递对象,则可以在正常条件下始终修改对象的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM