正如标题所示,我试图从指令和控制器更改范围变量。但是它不能改变controller / directive的值,这里是jsfiddle
如果单击Change
按钮,它将从控制器更改两个范围变量。
然后点击changed from controller
按钮(现在是指令)时,它会更改drop
变量但不会更改select
变量,而Change
按钮无法再次更改变量
我还尝试按照here所述传递变量,但没有 成功。
指令:
app.directive("drop", [function() {
return {
scope: true,
restrict: 'A',
link: function(scope, element, attrs) {
return element.bind('click', function(e) {
scope.drop = "changed to drop";
scope.select = "changed to select";
scope.$apply();
});
}
};
}]);
控制器:
function myController($scope) {
$scope.drop = "Initial value";
$scope.select = "Initial Value";
$scope.selection = function() {
$scope.drop = "changed from controller";
$scope.select = "changed from controller";
};
}
修改
更多测试显示,当在指令中更改范围变量时,控制器不能再更改它..
答案 0 :(得分:1)
删除指令的scope
属性值,它为指令提供了自己的范围