Camila Camila - 2 years ago 76
AngularJS Question

I replaced $scope to a variable and now my angular application doesn't work

I'm doing an Angular application and I read that it's a good practice don't use $scope. So I replaced to a variable, and now the data is not appearing on my page:

It was like this:

observatoryApp.controller('AppController', function AppController($scope, $http){
var $scope = this;
$scope.years = [];
$scope.years =;
$ = $scope.years[0];

Now is like this:

observatoryApp.controller('AppController', function AppController($http){
var self = this;
self.years = [];
self.years =; = self.years[0];

And the data are not showing on the index.html page:

<body ng-controller="AppController">
<select class="year form-control"
ng-options=" for year in years"

What I'm doing wrong?

Answer Source

I read that it's a good practice don't use $scope

That would have been in context of using controllerAs alias as alternative.

Unless you use that alias you use the injected $scope to assign model properties to. You would never do var $scope = this;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download