Angular: Empty Fiddle
http://angularjs.org/
HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-controller="MyCtrl">
<div my-messages></div>
</div>
JavaScript
var myApp = angular.module('myApp', []);
function MyCtrl ($scope) {
// Does nothing
}
myApp.directive('myMessages', function () {
return {
template: '<div my-objects objects="messages"></div>',
controller: function ($scope, messageManager) {
$scope.messages = [];
this.loadData = function () {
messageManager.getMessages().then(function (data) {
$scope.messages = data.messages;
console.log('Inside "loadData", $scope.messages =', $scope.messages);
});
};
},
link: function (scope, element, attrs, ctrl) {
ctrl.loadData();
}
};
});
myApp.directive('myObjects', function () {
return {
template: '<ul><li ng-repeat="object in objects">{{object.author}}</li></ul>',
scope: {
objects: '='
},
link: function (scope, element, attrs) {
scope.$watch('objects', function (objects) {
console.log('In $scope.$watch on "objects", array =', objects);
angular.forEach(objects, function (object) {
// I realize this should probably be in a filter, but this is a
// contrived example and doesn't really state what I'm ultimately
// doing with this data. It's just here to get a point across.
object.author = object.firstName + ' ' + object.lastName;
});
}, true);
}
};
});
myApp.factory('messageManager', function ($q) {
return {
getMessages: function () {
var deferred, promise;
deferred = $q.defer();
promise = deferred.promise;
setTimeout(function () {
deferred.resolve({
messages: [{
firstName: 'Sam',
lastName:...