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

标签 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 的作用域函数时,没有其他事情发生。应该再次调用,对吗?

编辑后说:我在页面上有这个,它确实表明在我调用作用域方法后变量被设置:

Workflow----> {{currentContext.workflow}}

最佳答案

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

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

在此处了解有关观察者和观察者以及如何使用它们的更多信息: Ben naddal tutorial on observers and watchers

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

关于javascript - 更新范围时不会调用 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243279/

相关文章:

javascript - 重写反向代理页面上的所有链接

javascript - 我们能否将大型 ajax 调用拆分为多个较小的调用以更快地加载数据?

javascript - eclipse 2020-06 语法突出显示不适用于 php 或 html 文件中的内联 javascript

angularjs - 将 angularjs 和 node.js 后端与 Cloudant DB 搜索 API 结合使用

javascript - 使用 unshift 和 pop 序列处理对象数组

javascript - 使用 Javascript 配置 Parsley,而不是 html 数据属性

javascript - 使用 AngularJS 自定义 HTML5 视频播放器控件

angularjs - 错误: key $$hashKey must not start with '$' (AngularJS)

javascript - 当我在表格元素上滚动时 ion-scroll 上的 stopPropagation

javascript - 循环 for -javascript