简体   繁体   English

更新范围时未调用Angular指令

[英]Angular directive not getting called when scope is updated

I have a scope that is modifying a variable: 我有一个正在修改变量的范围:

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

I have a directive on the page: 我在页面上有一条指令:

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

I have a directive: 我有一条指令:

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");
            }
      ....

When I load the page, I get the alert that the workflow is null. 加载页面时,我收到工作流为空的警报。

When I run the scope function that updates $scope.currentContext , nothing else happens. 当我运行更新$scope.currentContext的作用域函数时,没有任何其他反应。 It should be called again, right? 应该再次调用吧?

Edited to say: I have this on the page and it does show that the variable is getting set after I call the scope method: 编辑说:我在页面上有这个,它确实表明在调用scope方法后变量已被设置:

Workflow----> {{currentContext.workflow}} 工作流程----> {{currentContext.workflow}}

Defering to charlietfi, try with a watcher 视查理菲为准,与观察者一起尝试

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

You will have to watch/observe for the property change in the link function like this: 您将必须在链接函数中观察/观察属性变化,如下所示:

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

Read more about observers and watchers and how you can use them here: Ben naddal tutorial on observers and watchers 在此处详细了解观察者和观察者以及如何使用它们: Ben naddal关于观察者和观察者的教程

Try this gist: Gist for observers and watchers 尝试以下要点: 观察者和观察者的要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM