So I have a directive with isolate scope and a controllerAs pattern.
var directive = {
restrict: 'E',
scope: {
something: '='
},
templateUrl: './App/directiveTemplate.html',
controller: directiveController,
controllerAs: 'vm',
bindToController: true
}
and in the controller I init with a call to a REST service using $http that returns a promise.
function directiveController(someService) {
var vm = this;
// Here vm.something is defined and bound to the appropriate model set where the directive is used
init()
function init() {
return someService.getProducts()
.then(productsReady);
function productsReady(response) {
vm.products = response;
//find product using vm.something
// here vm.something is undefined
return vm.products;
}
}
The problem is that if I breakpoint before the init()
method vm.something
is defined like it should be but in the productsReady
function it is undefined.
Is that a normal behaviour? Is the promise resolving code in a different scope?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…