为什么我得到带有服务angularjs的$$状态变量

时间:2017-03-27 16:57:43

标签: angularjs

我想从我的控制器获取变量中的我的服务,但它给了我一个$$状态对象

这是我的控制器

$scope.myDataSMS = ServiceSms.async().then(function(data){
    $scope.myDataSMS1 = data;
    console.log($scope.myDataSMS1);

    return $scope.myDataSMS1;
});

console.log($scope.myDataSMS);

和我的服务

routeAppControllers.factory('ServiceSms', function($http,Token) {

    var key = Token.CreateToken()

    var myService = {

        async: function() {

            var data = 'token=' + encodeURIComponent(key);

            var promise =  $http({
                method: 'POST',
                url: 'PhpFunction/getsms.php',
                data: data,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })

                .then(function(data) {

                    // The then function here is an opportunity to modify the response
                  //   console.log(data.data);

                    // The return value gets picked up by the then in the controller.
                    return data.data;
                })

            // Return the promise to the controller
            return promise;
        }
    };
    return myService;
});

我认为问题是有希望的,但我有点卡住

如果有人可以帮助我

提前致谢

1 个答案:

答案 0 :(得分:1)

这是写下你的承诺的更好方式:

<强>控制器:

.controller('nameofcontroller', ['$scope', 'ServiceSms', function($scope, ServiceSms) {
    $scope.myDataSMS = ServiceSms.async()
      .then(
        function(data){
          $scope.myDataSMS1 = data;
          console.log($scope.myDataSMS1);
          return $scope.myDataSMS1;
        },
        function(err){
          console.log('err: ' + err);
        });
}]);

<强> SERVICE:

routeAppControllers.factory('ServiceSms', function($http,Token) {
   return {
      async: function() {
        var data = 'token=' + encodeURIComponent(key);
        return $http({
           method: 'POST',
           url: 'PhpFunction/getsms.php',
           data: data,
           headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        });
      }
    }
});