简体   繁体   English

$state.go 不能在单个控制器中工作

[英]$state.go not working in single controller

everyone.每个人。 I am having a strange problem...I have several states in my app, and they all seem to work except inside of one controller.我遇到了一个奇怪的问题......我的应用程序中有几个状态,除了在一个控制器内部之外,它们似乎都可以工作。 I have posted the factory whose function I am trying to use, along with the controller, and the state, and the view that is supposed to trigger the function:我已经发布了我尝试使用其功能的工厂,以及控制器、状态和应该触发该功能的视图:

app.factory('handleShipping', ['$http', function($http){
var handleShipping = {};
handleShipping.validate = function(address){
    return ($http.post('/shipping/validate',address).success(function(data){
        console.log(JSON.stringify(data));
    }));
}
    return handleShipping;

}]);

... ...

app.controller('ValidateCtrl',['$scope','$state', 'handleShipping','shipperName', 'auth',function($scope, $state, handleShipping, shipperName,auth){

    $scope.getShipper = function(shipName){
        shipperName.shipper = shipName;
    };


    $scope.validateAddress = function(){
        handleShipping.validate($scope.user).error(function(error){
            $scope.error = error;console.log('Error!');
        }).then(function(){
            $state.go('label');
        });
    };
}]);

... ...

 .state('address', {
                url: '/address', 
                templateUrl: '/address.html',
                controller: 'ValidateCtrl'
        })
 .state('label', {
               url: '/label', 
               templateUrl: '/label.html', 
               controller: 'LabelCtrl'
        });

... ...

<script type="text/ng-template" id="/address.html">
  <div class="page-header">

    <h1>Address, please</h1>

  </div>

  <div ng-show="error" class="alert alert-danger row">
    <span>{{ error.message }}</span>
  </div>

  <form ng-submit="validateAddress()"
    style="margin-top:30px;">


    <div class="form-group">
      <input type="text" class="form-control" placeholder="Street"
      ng-model="user.street"></input>
    </div>
    <div class="form-group">
      <input type="text"
      class="form-control"
      placeholder="City"
      ng-model="user.city"></input>
    </div>
    <div class="form-group">
      <input type="text" class="form-control" placeholder="State" ng-model="user.state"></input>
    </div>
    <div class="form-group">
     <input type="text"
      class="form-control"
      placeholder="Zip Code"
      ng-model="user.zipCode">
     </input>
   </div>
   <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</script>

I get no error message.我没有收到错误消息。 If I type localhost:3000/label, I can see the view.如果我输入 localhost:3000/label,我可以看到视图。 Also, I notice something sort of strange...if I stay on the view 'address', and I push the 'submit' button, nothing happens.此外,我注意到一些奇怪的事情……如果我停留在视图“地址”上,然后按下“提交”按钮,则什么也没有发生。 But if I leave the page alone, and the app running on terminal, it will randomly show the error message in the console, and on the terminal, I will see但是如果我不理会页面,而应用程序在终端上运行,它会在控制台中随机显示错误消息,而在终端上,我会看到

POST /shipping/validate - - ms - -

Any help would be greatly appreciated.任何帮助将不胜感激。

Factory method should return a promise.工厂方法应该返回一个承诺。 Currently you are using success & error callbacks inside service, It is not allowing you chain promise over validate function.目前您在服务内部使用successerror回调,它不允许您通过验证功能链接承诺。 You must be getting an error in console.您一定在控制台中收到错误消息。 $state.go isn't working because execution isn't went to that line. $state.go不起作用,因为没有执行到该行。

After returning promise you need to chain that promise using .then after validate method call.返回承诺后,您需要在验证方法调用之后使用.then链接该承诺。

Factory工厂

handleShipping.validate = function(address){
    return $http.post('/shipping/validate',address);
};

Controller控制器

$scope.validateAddress = function(){
    handleShipping.validate($scope.user).then(function(data){ //success
        //$state.go('label');
    }, function(error){ //error
        $state.go('label');
    })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM