Accessing Data in HTML

InstructorJohn Lindquist

Share this video with your friends

Send Tweet

In this lesson, John will take you from bad practices to good practices, as he explores how to bring data into your HTML templates with AngularJS. You'll go from attaching data directly to the $rootScope in your Angular module run block (bad!) all the way through using promises to asynchronously assign data to your controller and access it with "controller-as" syntax (much better!).

Vishal
~ 10 years ago

Hi

What is the better way to implement deleteTodo,

like I call ToDoService.deleteTodo(todoToBeDeleted).then( // splice scope copy of todos )

or

subscribe to an event (say TodoDeleted in ToDoService and then update splice local copy there ??

-Vishal

John Lindquistinstructor
~ 10 years ago

In your controller, if you assign a property on the controller to a property on a service (and you're using the controllerAs approach), Angular will automatically begin watching for changes. This is because using controllerAs puts the controller on the $scope and $scope keeps track of whatever it references.

So you shouldn't have to worry about events. There are rare scenarios where you don't want your model/view to stay in sync whenever a property changes and only update on certain events.

Helio
~ 9 years ago

There is another way you didn't talk about: Object.defineProperty.

Basically, in your controller you do something like this:

Object.defineProperty('todos', { get: function () { return TodosService.todos } });

Now everytime your TodosService is getting new todos, the controller will be updated as well.

Or you can inject the TodosService in the $scope.