angularjs - Angular: Using $scope vs calling a controller attribute -
i've been reading ng-book , worked through code school angular course other day , confused when use $scope
in controller , when use attribute of controller.
in code school course, controllers setup this:
app.controller('librarycontroller', function(){ this.books = getbooks //some function gets array });
but in ng-book , elsewhere, i've seen done scope:
app.controller('librarycontroller', function($scope){ $scope.books = getbooks //some function gets array })
from can tell, these 2 approaches same. first used in view this:
<div ng-controller="librarycontroller libraryctrl"> <ul> <li ng-repeat="book in libraryctrl.books">
while second be
<div ng-controller="librarycontroller"> <ul> <li ng-repeat="book in books">
am not understanding fundamental here? there difference in these 2 approaches , why $scope
approach used exclusively?
i see 2 reasons why scope approach used more controller as
approach:
- the scope approach 1 has been existing long time. it's since version 1.2 (iirc) "controller as" syntax introduced. people got used traditional way, example written way.
- the handling of
this
in javascript nightmare (imho). have read documentation of ever callback function used make surethis
doesn't change when callback called. using scope doesn't have problem.
Comments
Post a Comment