[英]2 way data binding directive angular
我混淆了角度的2路数据绑定。 看看代码吧! var bah
可以访问父对象$scope.something
但是当我单击按钮时,控制器中的值更改为false
但不是指令。 怎么了? 那是一个bug吗?
怎么解决这个? 感谢帮助我,希望你能写一个例子或参考链接
HTML
<div ng-controller="myController">
show me something {{ something }} <br>
<button ng-click="toggleSomething"><button>
<!-- this is a canvas -->
<my-directive></my-directive>
</div>
JS
angular.module('fooBar',[]).controller('myController', ['$scope', function($scope) {
// this is the something
$scope.something = true;
$scope.toggleSomething = function(){
if($scope.something) {
$scope.something = false
} else {
$scope.something = true
}
}
}]).directive('myDirective', function(){
return {
template: '<canvas width="500px" height="500px"></canvas>',
link: function(scope, el, attr) {
//how to access that 'something'
var bah = scope.$parent.something;
}
};
});
更新非常感谢大家。 特别是对你@immirza
我很抱歉,我不能一一回复你。 它只是添加$parent
//how to access that 'something'
var bah = scope.$parent.something
您可以直接访问$scope.something
在myDirective
不使用$parent
,因为该指令已shared scope
并且对于你的问题,如果你试图检测指令内的something
变化你不能只是放一个console.log($scope.something)
并检查因为它只执行了一次并且在点击之后它不再打印了,那不是“T意味着something
里面是改变不了的directive
。
并且你在ng-click
犯了一个错误,比如ng-click="toggleSomething"
它应该是ng-click="toggleSomething()"
因为你调用的函数不仅仅是一个变量。
这是一个DEMO
我已将<h1> ... {{ something }}</h1>
放在指令模板中,以显示某些内容在指令中也按预期工作。
通过这个优秀的指令系列
我已经为你的代码添加了一个plunker并添加了对指令的双向绑定。 你可以在plnkr看到它
angular.module('fooBar',[]).controller('myctr', ['$scope', function($scope) {
// this is the something
$scope.something = true;
$scope.toggleSomething = function(){
if($scope.something) {
$scope.something = false
} else {
$scope.something = true
}
}
}]).directive('myDirective', function(){
return {
//changed canvas to span so for simplixity.
template: '<span width="500px" height="500px">{{blah}} --- {{ dsomething }}</span>',
scope: { dsomething: "=" },
link: function(scope, el, attr) {
//watching $parent.variable is not recommonded as it makes your
//directive less reusable and forces who ever wants to use your
//directive to create the variable. dsomething will just update
//nicely w/o any code in this link function. the code below is just to demonstrate
//how to get it done by using $watch with $parent scope.
//how to access that 'something'
if(!scope.dsomething){
scope.dsomething = "something";
}
//because blah is a local variable and not a two-way binding variable
//we need to use $watch to update the value.
//you can use "dsomething" instead of "$parent.something"
//
scope.$watch("$parent.something", function(newVal, oldVal){
scope.blah = newVal;
})
}
};
});
你可以使用你的指令:
<div ng-controller="myctr">
show me something {{ something }} <br />
<button ng-click="toggleSomething()">Update Something</button>
<button>
<!-- this is a canvas -->
<my-directive dsomething="something"></my-directive>
</button>
</div>
注意ng-click =“toggleSomething()”。 这是一个不传递函数的函数调用。 ng-click =“toggleSomething”将无效。
问题是你误解了双向数据绑定是什么,基本上任何与指令two way bound
元素都可以由控制器或指令更新,而另一个元素会立即看到这个更改反映在其中。
当你使用$parent
访问它时,你强行读取指令中的值,仅此而已,没有人告诉指令重新评估var bah = scope.$parent.something
因为scope.something
的值已经更新了父级控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.