Dan
Dan

Reputation: 463

$scope not getting defined properly

I am trying to attach notes to $scope instead of controller(this) in the following code:

angular.module('NoteWrangler')
.controller('NotesIndexController', ['$http', function($http) {
  var controller = this;


  $http({method: 'GET', url: '/notes'}).success(function(data) {
    controller.notes = data;
  });
}]);

So, when I define $scope like the following :

.controller('NotesIndexController', ['$scope', function($scope)

It doesn't work as I would need to define $http somewhere in order to use it in the code? So, where exactly I should define $scope then?

I have referred this documentation.

Upvotes: 0

Views: 41

Answers (3)

Claies
Claies

Reputation: 22323

You don't "define" $scope, it is a framework object that is managed for you.

If you use the ControllerAs Syntax, your controller can be added to $scope as a property. Otherwise, you would need to refer to the $scope object that is injectable through Angular Dependency Injection. You can inject as many dependencies as you need for your app.

So, the two options which you have available to you are:

ControllerAs:

angular.module('NoteWrangler')
.controller('NotesIndexController', ['$http', function($http) {
  var controller = this;


  $http({method: 'GET', url: '/notes'}).success(function(data) {
    controller.notes = data;
  });
}]);

<div ng-controller="NotesIndexController as controller">
    {{controller.notes}}
</div>

Direct $scope:

angular.module('NoteWrangler')
.controller('NotesIndexController', ['$scope', '$http', function($scope, $http) {
  var controller = this;


  $http({method: 'GET', url: '/notes'}).success(function(data) {
    $scope.notes = data;
  });
}]);

<div ng-controller="NotesIndexController">
    {{notes}}
</div>

Note that the $scope object is implicit in the first example and not required in the controller code, and explicit in the second. Also note, $scope has some specific rules regarding primitives and Prototype Inheritance, so it is always recommended to use a dot in your HTML bindings, which the ControllerAs syntax enforces automatically.

Ultimately, the choice of which syntax to use is up to you, but it's recommended to be consistent throughout your application, and either always reference $scope or only reference it for special properties you can't reach any other way.

Upvotes: 1

Ali Adravi
Ali Adravi

Reputation: 22743

Not sure why you are using var controller = this; but you have not injected $http

You can use it like

.controller('NotesIndexController', ['$scope', '$http', function($scope, $http){
   $http({method: 'GET', url: '/notes'})
      .success(function(data) {
       $scope.notes = data;
    });
}]);

Upvotes: 0

Moncef Hassein-bey
Moncef Hassein-bey

Reputation: 1361

 angular
    .module('NoteWrangler')
    .controller('NotesIndexController', ['$scope', '$http', function($scope, $http)  {

       $http({method: 'GET', url: '/notes'}).success(function(data) {
          $scope.notes = data;
       });

    }]);

Upvotes: 0

Related Questions