簡體   English   中英

更新范圍時未調用Angular指令

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

我有一個正在修改變量的范圍:

$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的作用域函數時,沒有任何其他反應。 應該再次調用吧?

編輯說:我在頁面上有這個,它確實表明在調用scope方法后變量已被設置:

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

視查理菲為准,與觀察者一起嘗試

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

您將必須在鏈接函數中觀察/觀察屬性變化,如下所示:

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

在此處詳細了解觀察者和觀察者以及如何使用它們: Ben naddal關於觀察者和觀察者的教程

嘗試以下要點: 觀察者和觀察者的要點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM