2014-09-03 53 views
0

我有一个angularJS应用程序,它使用服务在3个控制器之间共享数据。我使用html表单中的按钮在视图之间路由。表单的action属性设置为目标路由的URL。在Chrome中使用HTML表单操作更改路线(AngularJS)

在Firefox似乎没有成为一个问题,但镀铬插入一个问号到URL,这似乎重新启动应用程序并删除共享服务:

火狐结果:

“HTTP://本地主机/对myApp /应用/#/ thirdScreen”(没问题)

铬结果:

“HTTP://本地主机/对myApp /应用/#/ THI rdScreen“(丢弃服务,应用程序似乎重新启动并且共享服务数据丢失)。

但是,连续来回连续数次似乎解决了Chrome中剩余应用程序生命周期的问题。

  • 问号是什么意思?
  • 是否必须使用我的控制器中的$ location服务来改变路线,还是有另一种方法?

谢谢!

的Html

//Extract from the bottom of the secondScreen.html partial 

<div class="col-md-12 column"> 
      <form action="#/thirdScreen"> 
       <button type="submit" class="btn btn-lg btn-block btn-danger"> 
           Go to third page 
       </button> 
      </form> 
</div> 

App.js

angular.module('myApp', [ 
    'myApp.services', 
    'myApp.directives', 
    'myApp.controllers', 
    'myApp.filters', 
    'ngRoute' 
]). 
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 
    $routeProvider. 
     when("/firstScreen", {templateUrl: "partials/firstScreen.html", controller: 'firstScreenController'}). 
     when("/secondScreen", {templateUrl: "partials/secondScreen.html", controller:"secondScreenController"}). 
    when("/thirdScreen", {templateUrl: "partials/thirdScreen.html", controller:"thirdScreenController"}). 
otherwise({redirectTo: '/firstScreen'}); 
}]); 

Controllers.js

controller('secondScreenController', function($scope, $http, sessionDetailsService, modalService, goodsCheckedInFetchingService) { 

    $scope.session = sessionDetailsService; 

}). 

controller('thirdScreenController', function($scope, $http, sessionDetailsService, modalService, goodsCheckedInFetchingService) { 

    $scope.session = sessionDetailsService; 

}). 
+0

使用,适用于角导航是没有意义的 – charlietfl 2014-09-03 11:44:44

+0

请澄清一下,是不是因为你应该只使用一种形式提交数据到服务器并加载新页面?我没有使用href的原因是因为它没有被允许在一个按钮元素中,可能我错了吗? – emmet 2014-09-03 12:10:04

+0

为什么不使用''标签? – charlietfl 2014-09-03 12:11:21

回答

0

我认为你必须设置$locationProvider.hashPrefix('?')在你的代码是这样的:

angular.module('myApp', ['ngRoute']) 
.config(['$locationProvider', function($locationProvider) { 
$locationProvider.html5Mode(false); 
$locationProvider.hashPrefix('?'); 
}]); 

删除此$locationProvider.hashPrefix('?');从您的代码。

,或者你没有设置这个然后把这个代码在app.js:

angular.module('myApp', ['ngRoute']) 
    .config(['$locationProvider', function($locationProvider) { 
    $locationProvider.html5Mode(false); 
    $locationProvider.hashPrefix(''); 
    }]); 
+0

抱歉没有效果,我认为问题在于表单元素在Chrome中刷新页面。用标签替换表单元素解决了问题。谢谢! – emmet 2014-09-03 12:25:09