控制器和工厂之间的链接

时间:2016-02-22 15:13:17

标签: angularjs angularjs-scope angular-services

.controller('LoginConnect', ['$scope', 'connecting',
function($scope, connecting){
    $scope.user = {};
    $scope.connect = function(){
      connecting();
    };

  }
])
.factory("connecting", ["$scope", "$q", "$http", function ($scope,$q, $http){       var deferred = $q.defer();
       $http({
           method: 'POST',
           url: "http://api.tiime-ae.fr/0.1/request/login.php",
           headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           transformRequest: function(obj) {
               var str = [];
               for(var p in obj)
               str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
               return str.join("&");
           },
           data: {login: $scope.user.login, password: $scope.user.password}
           })
       .success(function(result){
          deferred.resolve(result);
          var promise = deferred.promise;
          promise.then(function(result){
            jsonTab = angular.fromJson(result);
             $scope.result = result["data"];
         $scope.user.token = result["data"];
           });
         })

    }])

这里是HTML:

    <!-- User Connection  -->
<form name="userConnect" ng-submit="connect()" novalidate ng-controller="LoginConnect">
  <label>
    Enter your name:
    <input type="text"
           name="myEmail"
           ng-model="user.login"
            />
  </label>

    <label>
    Enter your Password:
    <input type="password"
           name="password"
           ng-model="user.password"
            />
  </label>

<input type="submit" value="Connection">
<p>resultat : {{result}}</p>
<p ng-model="user.token">
  token : {{mytoken}}
</p>


<p ng-model="user.datab">
  datas : {{datab}}
</p>
<br><br><br>


</form>

嗨,我是Angular JS的新手,你能不能帮我解决这个问题。我有以下错误:$ scopeProvider&lt; - $ scope&lt; - 连接 我认为这个错误是由于控制器和工厂之间的链接

3 个答案:

答案 0 :(得分:1)

您的服务需要退货。 而是尝试:

.factory("connecting", ["$scope", "$q", "$http", function connect($scope,$q, $http){       
    return function() { 
       //your method
    };
}])

答案 1 :(得分:0)

以下是与您类似的链接控制器和工厂的代码示例。你只需从工厂返回一些东西。

.controller('LoginConnect', ['$scope', 'connecting',
                function($scope, connecting) {
                    $scope.user = {};
                    $scope.connect = function() {
                        connecting.login($scope.signupData.username, $scope.signupData.password).success(function(data) {
                                // 
                                $sessionStorage.userid = data.UserId;
                                $sessionStorage.token = data.access_token;
                                if (data.UserId != "") {
                                    $state.go('app.editProfile');
                                }
                            })
                            .error(function(data, status, headers, config) {
                                console.log("http error", data);
                            });
                    };

                }
            ])
            .factory('connecting',  ["$scope", "$q", "$http", function ($scope,$q, $http){       

                var urlBase = 'http://apple.com:7071';

                //Login
                return {
                    login: function(loginusername, loginpassword) {

                        return $http({
                            method: 'POST',
                            url: urlBase + '/Login',
                            data: {
                                username: loginusername,
                                password: loginpassword,
                                grant_type: 'password'
                            },
                            transformRequest: function(obj) {
                                var str = [];
                                for (var p in obj)
                                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                                return str.join("&");
                            }
                        })

                    }
                }
            }])

答案 2 :(得分:0)

${user_test}需要返回Fatcory

Object