I'm trying to learn to use controller as
more often in my angular controllers and ran into a scoping issue when updating a scope from inside another function:
.controller('profileCtrl', function(ProfileData, Restangular){
// ProfileData comes from router.resolve
this.user = ProfileData;
// I want to update the data with a button click or something
this.refresh = function(id){
Restangular.one('users', id).get().then(function(resp){
this.user = resp;
})
}
});
The this.user = resp
doesn't actually refer to the same this
in this.user = ProfileData
. I don't know what it refers to, maybe the function?
I fixed this by injecting scope and then changing my update function:
this.refresh = function(id){
Restangular.one('users', id).get(params).then(function(resp){
$scope.profile.user = resp;
})
}
The problem I have with this approach is that it depends on me calling the controller myCtrl as profile
.
Is there a way to update this.user
from within my function without injecting scope?