イベントjsonデータの取得を担当するコントローラーがあり、データがある場合はデータでdomを更新し、そうでない場合はエラーメッセージでdomを更新します。
//Controller.js
myApp.controller('EventsCtrl', ['$scope','API', function ($scope, api) {
var events = api.getEvents(); //events: {data: [], error: {message: 'Some message'}}
}]);
//Directives.js
myApp.directive('notification', function () {
return {
restrict: 'A',
link: notificationLink
};
});
/**
* Creates notification with given message
*/
var notificationLink = function($scope, element, attrs) {
$scope.$watch('notification', function(message) {
element.children('#message').text(message);
element.slideDown('slow');
element.children('.close').bind('click', function(e) {
e.preventDefault();
element.slideUp('slow', function () {
element.children('#message').empty();
});
});
});
};
//Services.js
...
$http.get(rest.getEventsUrl()).success(function (data) {
// Do something with data
}).error(function (data) {
$window.notification = data;
});
問題は、要素の変更がトリガーされることですが、 $window.notification には何も含まれていません。
編集:$watchで試してみました。
編集: html の両方のセットを 1 つのコントローラーに移動した後、DOM 操作は $watch() で機能します。お二方、ご協力ありがとうございました!