admin管理员组文章数量:1289412
Okay, this is really bugging me. I have a directive with isolate scope, using the controllerAs
syntax and bindToController
:
function exampleDirectiveFactory() {
var bindings = {
foo: '=',
bar: '@'
}
return {
bindToController: true,
controller : 'ExampleController',
controllerAs : 'vm',
scope : bindings,
template : 'foo = {{ vm.foo }}<br />bar = {{ vm.bar }}'
};
}
Assuming a usage like this:
<example foo="FOO" bar="BAR"></example>
...I expect the value of vm.foo
to be two-way bound to the value of the foo
attribute. Instead, it is undefined
.
The value of vm.bar
is equal to the attribute value bar
of the HTML element, which I expect.
When I try to change the value of vm.bar
using a filter, no change persists.
When I store the filtered value of vm.bar
to a new variable, vm.baz
, that works as expected.
Here is a fiddle
So my question has two parts:
A) Why is the value of vm.foo
undefined when using '='
?
B) Why can't I change the value of vm.bar
in the scope of the controller, even if that change does not propagate to the HTML element attribute (which it shouldn't, because I'm using '@'
)?
Okay, this is really bugging me. I have a directive with isolate scope, using the controllerAs
syntax and bindToController
:
function exampleDirectiveFactory() {
var bindings = {
foo: '=',
bar: '@'
}
return {
bindToController: true,
controller : 'ExampleController',
controllerAs : 'vm',
scope : bindings,
template : 'foo = {{ vm.foo }}<br />bar = {{ vm.bar }}'
};
}
Assuming a usage like this:
<example foo="FOO" bar="BAR"></example>
...I expect the value of vm.foo
to be two-way bound to the value of the foo
attribute. Instead, it is undefined
.
The value of vm.bar
is equal to the attribute value bar
of the HTML element, which I expect.
When I try to change the value of vm.bar
using a filter, no change persists.
When I store the filtered value of vm.bar
to a new variable, vm.baz
, that works as expected.
Here is a fiddle
So my question has two parts:
A) Why is the value of vm.foo
undefined when using '='
?
B) Why can't I change the value of vm.bar
in the scope of the controller, even if that change does not propagate to the HTML element attribute (which it shouldn't, because I'm using '@'
)?
1 Answer
Reset to default 111.4 changed how bindToController works. Though it appears that angular's documentation is still referring to the field as true
/false
. Now it can accept an object just like the scope
attribute where the attributes indicate what you want bound and how to bind it.
function exampleDirectiveFactory() {
var bindings = {
foo: '=',
bar: '@'
}
return {
bindToController: bindings, //<-- things that will be bound
controller : 'ExampleController',
controllerAs : 'vm',
scope : {}, //<-- isolated scope
template : 'foo = {{ vm.foo }}<br />bar = {{ vm.bar }}'
};
}
In addition, in your fiddle, FOO is undefined
on the parent scope, so when it binds, it will pull that undefined
into the directive's bound controller's scope.
Further reading:
One major thing that this new bindToController
syntax allows is the ability for the directive to not be an isolated scope and still identify what to bind. You can actually set scope to true
on your directive to have a new child scope that will inherit from it's ancestors.
本文标签:
版权声明:本文标题:javascript - AngularJS 1.4: How to create two-way binding using bindToController and controllerAs syntax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741444068a2379090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论