發現了幾個人有類似的問題,但通常的作用域。$ apply不適用於我($ digest已在進行中)。今天讓我感動。
請在下面找到我的指令的簡化版本。
- initialize()將myValue設置爲其初始值。
- $ rootScope事件被捕獲。
- 承諾正在解決。
- $ localStorage更新
- console.log(myValue)提供正確的值。
但是我的視圖未更新。這是怎麼回事?!
(function() {
'use strict';
angular
.module('myApp')
.directive('myDirective', myDirective);
function myDirective($rootScope, $localStorage, MyPromise, MyService) {
var linkFunction = function (scope, element, attributes) {
scope.myValue = null;
$rootScope.$on('someEvent', function (event, data) {
MyPromise().get({}, function (result) {
$localStorage.result = result;
scope.myValue = MyService.getFromLocalStorageAndReturnStuff();
console.log(scope.myValue);
});
});
initialize();
function initialize() {
scope.myValue = MyService.getFromLocalStorageAndReturnStuff();
}
};
return {
restrict: 'A',
transclude: true,
replace: true,
scope: {
someProperty: '=myProperty'
},
link: linkFunction,
templateUrl: 'myTemplate.html'
};
}
})();
{{myValue}}
嘗試清除緩存 –