【发布时间】:2017-04-15 14:39:43
【问题描述】:
Device返回数据时如何调用回调并将this传递给回调方法。
控制器
(function() {
'use strict';
angular
.module('frontendApp')
.controller('DeviceController', DeviceController);
DeviceController.$inject = ['$scope', '$state', 'Device'];
function DeviceController ($scope, $state, Device) {
var vm = this;
vm.devices = [];
loadAll();
function updateMap(flag){
var self = this;//how to pass "this" from loadAll()?
// logic to update map
}
function loadAll() {
Device.query(function(result) {
vm.devices = result;
// Callback function here - updateMap(true)
});
}
}
})();
服务
function Device ($resource, DateUtils) {
var resourceUrl = 'api/devices/:id';
return $resource(resourceUrl, {}, {
'query': { method: 'GET', isArray: true},
'update': { method:'PUT' }
});
}
【问题讨论】:
-
你能解释一下
this指的是什么,控制器、虚拟机还是服务? -
this引用控制器。我正在尝试从updateMap调用控制器中定义的其他方法。 -
您可以直接在
updateMap中使用vm。我没有发现任何问题。可以试试吗? -
谢谢。它奏效了。