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.

3

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.