0

我有一个奇怪的事情:

我的控制器可以工作,但是在加载时调用 getHash 服务,而不是saveit()在索引 html 上调用函数:

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

谢谢,帕特里克

4

0 回答 0