If the point of using this sytax: <div ng-controller="BuildingsCtrl as bc">
is to avoid using $scope
(and apparently it is), then how should I go about using $http
?
That is, how could I re-write the following code to not use $scope?
angular.module('atlasAngularApp')
.controller('BuildingsCtrl', function ($scope, $http) {
this.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
this.getBuildings = function () {
$http.get('http://localhost:40602/api/1.0/buildings')
.then(function successCallaback(response) {
======> $scope.buildings = response.data;
}, function errorCallback(response) {
alert("Error");
}
);
}
});
To elaborate a little more,
<li ng-repeat="thing in bc.awesomeThings">
{{ thing }}
</li>
Works fine with this.awesomeThings
, so a view can use this
, but the following doesn't work:
angular.module('atlasAngularApp')
.controller('BuildingsCtrl', function ($http) {
var self = this;
this.getBuildings = function () {
$http.get('http://localhost:40602/api/1.0/buildings')
.then(function successCallaback(response) {
======> self.buildings = response.data;
}, function errorCallback(response) {
alert("Error");
}
);
}
});
(notice the self.buildings
bit.)
I've tried a number of variations along these lines theme, but nothing so far has worked. This question is similar, but I wasn't able to adapt it to my code.
I should probably add that I don't have anything against $scope
, I'm just trying to do things the way yeoman-generated angular seems to approve of. I'd also like some explanation on why $scope
could be considered a bad thing.
For completeness, here's my view. Maybe there's something wrong with it?
<div ng-controller="BuildingsCtrl as bc">
<table ng-init="buildings = bc.getBuildings()">
<tr ng-repeat="building in buildings">
<td>{{ building.name }}</td>
<td>{{ building.code }}</td>
<td>{{ building.image }}</td>
</tr>
</table>
</div>
The code does work, so long as I use $scope