更改控制器和指令之间的范围变量

时间:2016-02-22 12:29:30

标签: angularjs scope directive

正如标题所示,我试图从指令和控制器更改范围变量。但是它不能改变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";
  };
}

修改

更多测试显示,当在指令中更改范围变量时,控制器不能再更改它..

1 个答案:

答案 0 :(得分:1)

删除指令的scope属性值,它为指令提供了自己的范围