2016-01-26 74 views
0

我有一個簡單的指令顯示通知,用戶可以點擊它關閉它。它不像預期的那樣工作(一旦用戶隱藏它不再顯示)離子,並且它只在角度上工作。爲什麼範圍不更新一旦指令改變了離子

Angular Demo

Ionic CodePen

這裏是指令代碼

angular.module('app').directive('notificationBar', [function(){ 
return { 
    restrict : 'A', 
    scope : { 
     msg : '@', 
     type : '@', 
     show : '=' 
    }, 
    template : '<div ng-click="hideIt()" ng-show="show" class="alert {{type}}"><span>&times;</span> {{ msg }}</div>', 
    link : function (scope, elem, attr) { 
     scope.type = attr.type || 'alert-danger'; 

     scope.hideIt = function() { 
      scope.show = false; 
     }; 

     // for debugging 
     scope.$watch('show', function(newValue, oldValue) { 
     console.info('old show val', oldValue); 
     console.info('new show val', newValue); 
     }); 

    } 
    }; 
}]); 

回答

2

貌似點規則在角JS是在這裏指責。因爲error是一個原始值,所以一旦在控制器範圍內發生了變化,指令正在監視的變量就是指舊的值。

爲什麼這是不同的網絡和離子,不知道。但要解決這個問題,請在對象內部跳轉error,並使用用戶dot notation訪問它。

像這樣:

$scope.error = { 
    error: true 
}; 

<div notification-bar show="error.error" msg="Something is wrong here :(" type="alert-danger"></div> 

下面是更新後的代碼筆:http://codepen.io/anon/pen/BjrRRJ?editors=1011

有關此dot rule我提到的更多信息,這裏是一個很好的計算器後,這是值得閱讀:https://stackoverflow.com/a/17607794/841804https://stackoverflow.com/a/14049482/841804

相關問題