我有一件奇怪的事情:
我的控制器工作,但getHash服务在加载时被调用,而不是在索引html上调用saveit()
函数:
angular.module('myApp.controllers', [])
.controller('MyCtrl', ['$scope','$http','getHash', function ($scope,$http, getHash) {
$scope.keys = getHash.get(function(keys) {
$scope.key = keys.key;
});
$scope.saveit = function () {
$scope.formData = {
'key' : $scope.key
};
console.log ($scope.formData);
//Do Something with formData
}
}
此示例无效,$scope.key
为空,但getHash
服务应按原样调用。
angular.module('myApp.controllers', [])
.controller('MyCtrl', ['$scope','$http','getHash', function ($scope,$http, getHash) {
$scope.saveit = function () {
$scope.keys = getHash.get(function(keys) {
$scope.key = keys.key;
});
$scope.formData = {
'key' : $scope.key
};
console.log ($scope.formData);
//Do Something with formData
}
}
有没有人对此行为有任何解释?如何在函数$scope
中获得saveit
?
或将变量保存到$scope
内的saveit
?
谢谢,帕特里克