我有這個指令,我試圖讓它重新加載它自動獲取的內容。到目前爲止,沒有太多的運氣。小心幫忙?該指令永不重新加載其內容
編輯:我已經嘗試了建議的答案,但它不會工作。我得到一個$ digestx10錯誤。
一切工作正常,並保存好,DOM只是不會重新加載。
angular.module('mymodule').directive('chatContent',
["$compile",'localStorageService','loginService','peopleOnlineService',
function($compile, localStorageService, loginService, peopleOnlineService) {
var LoadData = function (data, userId) {
var template = '';
//localStorageService.IsUser(data.userId);
if(localStorageService.IsUser(userId) === true){
template = '<div class="feedItemChat"><div class="chatItem"><div class="chatMessage userChatMessage">{{content.chatMessage}}</div></div></div>';
}else{
template = '<div class="feedItemChat"><div class="chatItem"><div class="chatMessage otherChatMessage">{{content.chatMessage}}</div></div></div>';
}
return template;
};
var linker = function (scope, element) {
var data = localStorageService.LoadDataLocal(localStorageService.CheckCorrectRoom(loginService.GetUser().userId, peopleOnlineService.GetParams().userId));
scope.$watch(data, function(){
element.html(LoadData(data,scope.content.userId)).show();
$compile(element.contents())(scope);
});
};
return {
restrict: 'E',
link: linker,
scope: {
content: '='
}
};
}]);
我也想知道,如何在這裏插入html模板,而不是愚蠢的長字符串呢?
那麼你只設置可變數據一次,難怪它不會改變。將'LoadDataLocal ...'調用放入一個函數中,並將該函數用作手錶的第一個參數(所以當注入新的東西時你會注意到)。 – doldt
小心寫下它?我在嘗試,但它不起作用... –
嘗試用第三個參數「true」調用'$ watch'。 (第三個參數是'objectEquality') –