Skip to content Skip to sidebar Skip to footer

Angularjs Binding Two Variables Together Without Being Told To Do So

This is my Controller (home.js): angular.module('HomeApp', ['BaseApp']) .controller('MainCtrl', ['$http', '$window', 'BaseService', function($http, $window, BaseService) {

Solution 1:

In the case of BaseService.posts, you are first assigning it to an empty object and then assigning it to an entirely different object (response.data), which is presumably an array, in the postsY function. The controller will still have a reference to the empty object, and have no idea that the object has been replaced by the service with an array.

In the case of self.like(), That function is merely iterating through the existing array of posts and modifying the properties of the objects. If the controller is properly pointing to the array of objects, since the array itself isn't being removed and replaced, the controller will be pointing to the same instance of all the objects in the array in memory, and changes to these object's properties will be reflected.

Post a Comment for "Angularjs Binding Two Variables Together Without Being Told To Do So"