更新范围时不会调用Angular指令

时间:2016-02-06 16:19:34

标签: javascript angularjs

我有一个修改变量的范围:

$scope.showContext = function(context) {
        $scope.currentContext = context;
....
}

我在页面上有一个指令:

<div org-chart workflow="currentContext" />

我有一个指令:

var OrgChartDirective = function($timeout) {
    return {
        restrict : 'AE',
        scope : {
            workflow : '=',
        },
        link : function(scope, element, attrs) {


            if (scope.workflow != null) {
                console.log("**________________________________**");
                console.log(scope);
            } else {
                alert("Workflow is null");
            }
      ....

当我加载页面时,我收到工作流为空的警报。

当我运行更新$scope.currentContext的范围函数时,没有其他任何事情发生。它应该再次调用,对吧?

编辑说:我在页面上有这个,它确实显示我调用范围方法后变量已设置:

  
    

工作流----&GT; {{currentContext.workflow}}

  

2 个答案:

答案 0 :(得分:1)

推荐使用charlietfi,尝试使用观察者

link : function(scope, element, attrs) {
  scope.$watch('workflow', function(val) {
  ...
  });

答案 1 :(得分:1)

您必须观察/观察链接功能中的属性更改,如下所示:

attrs.$watch('workflow', function() {
    //do whatever logic you want
});

详细了解观察员和观察者以及如何在此处使用它们: Ben naddal tutorial on observers and watchers

试试这个要点:Gist for observers and watchers