控制器范围内的Angularjs函数

时间:2013-07-27 00:44:53

标签: function angularjs controller scope

我有一件奇怪的事情:

我的控制器工作,但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

谢谢,帕特里克

0 个答案:

没有答案