我已經被示出一個指令(或應)如果「的isEmpty」變量是真NG-顯示不指示工作
// index.html
<div empty-notes-screen coleccion="Notes" ng-show="isEmpty"></div>
// directive.js
angular.module('myApp')
.directive('emptyNotesScreen', function() {
return {
templateUrl: 'views/notes/emptynotesscreen.html',
replace: true,
scope : {
coleccion : '='
},
link : function (scope, elm,attrs){
scope.$watchCollection('coleccion', function(newValue) {
if(newValue.length>0){
scope.isEmpty = false;
}
else{
scope.isEmpty = true;
}
console.log('isEmpty: ',scope.isEmpty);
console.log(' coleccion has changed');
});
}
};
});
// views/notes/emptynotesscreen.html
<div class="white-text">
<div class="center grey darken-2" style="padding: 45px;">
<img width="180px" src="images/ignotize-white.png">
<h4>There's no notes yet!</h4>
<p>Please, add a note at least to see it here.</p>
</div>
</div>
另外,我有一個控制器,它看起來像這樣:
// scripts/index.js
angular.module('myApp')
.controller('IndexNoteCtrl', function ($scope, NoteResource) {
$scope.Notes = NoteResource.query();
});
結果是,scope.isEmpty相應地改變,但在HTML中,isEmpty永遠不會改變!所以HTML從不顯示或隱藏。 我不知道我爲什麼或在哪裏做錯了什麼。
可能是由於'''replace:true''' – Grievoushead
您是在調試控制器還是指令?如果一個指令 - 你嘗試手動把你的控制器'$ scope.isEmpty = true;'看看指令提供了什麼? – shershen
您的'isEmpty'從未出現在其中'ngShow'評估的父範圍中。 –