1

Which one is better? Why?

(function(){
  var app = angular.module('myApp', []);

  app.controller('MyController', function() {
    this.guy = obj1;
  });

  app.controller('AnotherController', function ($scope){
    $scope.guy = obj2;
  });

  var obj1 = {
    'name' : 'david',
    'title' : 'dude from obj1',
    'company' : 'AA',
    'doesIt' : 'this uses this'
  }, obj2 = {
    'name' : 'warren',
    'title' : 'dude from obj2',
    'company' : 'AA',
    'doesIt' : 'this uses scope'
  };
})();

I've seen tutorials that use both. Is this a preference thing? Is it just whether to be able to use the controller alias in the html attr or not? What's so great about $scope? I'm looking for a straight forward answer. Thanks.

davidpm4
  • 562
  • 1
  • 7
  • 22

1 Answers1

1

The main advantage of controller as syntax - it makes much cleaner html:

<div ng-contoller="parentController">
     <div ng-contoller="childController">
         <!-- you can't say exactly, where test located-->
         {{test}}  
     </div>
</div>

<div ng-contoller="parentController as parent">
     <div ng-contoller="childController as child">
         <!--it's clear where test-->
         {{parent.test}}  
     </div>
</div>

also you can see this