Why isn't my directive's selection being bound to the controller?

半城伤御伤魂 提交于 2019-12-04 12:53:52

By default, Angular creates a scope object (most commonly referred to with the variable $scope) for each HTML Template.

The scope: { selectedOption: '='}, in your code is actually creating an isolated scope for the directive, and making selectedOption a property on that scope object.

The line controllerAs: 'ctrl' is creating a property on this same scope object which points to the controller object.

This actually means that in the controller, you could technically access ctrl.$parent.selectedOption, which would return the selectedOption property of the ctrl object's parent, which is scope. In practice, however, this is very cumbersome.

In Angular 1.3, a new option was added, bindToController : true. This option automatically binds the properties from the scope: definition to the controllerAs: object instead of scope itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!