无法使用angular routeprovider重定向

时间:2015-02-26 13:56:28

标签: javascript angularjs ngroute route-provider

当我点击locahost时,当我无法重定向到signup.html页面时,我是角度新手:port / projectname /

虽然我在app.js中为singup指定了模板网址,但我得到了404

但是当我点击locahost:port / projectname / signup.html文件正在打开我做错了吗?

当我点击locahost时,我想获得signup.html:port / projectname /

app.js

var app = angular.module('pollApp', ['ngRoute']);
  app.config(['$routeProvider',function($routeProvider){
    $routeProvider
     .when('/Welcome', {
         templateUrl: '/Welcome.html',
         controller: 'submitCtrl'
       })
      .when('/', {
        templateUrl: '/signup.html',
        controller: 'submitCtrl'
       })
       .otherwise({
         redirectTo: '/Welcome.html'
       });
}]);

submitController.js

app.controller('submitCtrl',['$scope', '$http', '$location',

  function ($scope, $http, $location) {
    $scope.timeZones =      [
     { label: '(GMT-09:00) Alaska', value: 'Alaska' },
     { label: '(GMT-08:00) Pacific Time (US & Canada)', value: 'Pacific Time (US & Canada)' }
    ];  


    $scope.submit=function()    {
        var userDetails=new Object();
        userDetails.firstname=$scope.firstname;
        userDetails.lastname=$scope.lastname;
        userDetails.Companyname=$scope.Companyname;
        userDetails.Email=$scope.Email;
        userDetails.timezone=$scope.timezone;
        console.log(userDetails);
        $http({
          method: 'POST',
          data: userDetails,
          url:'/mongopractise/rest/signup/userdata',
          headers: {'Content-Type':'application/json'}
        }).success(function(data, status, headers, config) {

            console.log("success data"+JSON.stringify(data));
            $scope.username=data;
            $location.url('/Welcome.html').replace();;

          }).
          error(function(data, status, headers, config) {
              console.log("Failure data"+data);
          });
}

 }]);

Signup.html

              <html lang="en" ng-app="pollApp">
               <head>
               <meta charset="utf-8">
              <meta http-equiv="X-UA-Compatible" content="IE=edge">
             <meta name="viewport" content="width=device-width, initial-scale=1">
                <title>Welcome!</title>

                <script src="lib/angular.js"></script>
                <link href="css/bootstrap.min.css" rel="stylesheet">
                 <div class="container" ng-controller='submitCtrl'>

       <form class="form-horizontal" role="form">
                    <div class="form-group">
                                  <label class="col-lg-3 control-label">First name:</label>
                                    <div class="col-lg-8">
                    <input class="form-control" value="Jane" type="text" ng-      model="firstname">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Last name:</label>
                <div class="col-lg-8">
                    <input class="form-control" value="Bishop" type="text" ng-model="lastname">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Company:</label>
                <div class="col-lg-8">
                    <input class="form-control" value="" type="text" ng-model="Companyname">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Email:</label>
                <div class="col-lg-8">
                    <input class="form-control" value="janesemail@gmail.com" type="text" ng-model="Email">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Time Zone:</label>
                <div class="col-lg-8">
                    <div class="ui-select">
                        <select id="user_time_zone" class="form-control" ng-model="timezone" ng-options="timeZones as timeZones.label for timeZones in timeZones">

                        </select>
                    </div>
                </div>
            </div>

            <div class="col-md-8">
                    <input class="btn btn-primary" ng-click='submit()' value="Save Changes" type="button"> <span class=""></span>

                    <input class="btn btn-default" value="Cancel" type="reset">
                </div>
            </div>
        </form>

        </head>
      </html>

2 个答案:

答案 0 :(得分:0)

您的路线不包含&#39; / projectname&#39;,我认为这是您的应用程序上下文。您需要在路径网址中包含该内容以使其正常工作。

答案 1 :(得分:0)

您的端口之后的任何路径都应该在routeProvider配置中。您的服务器应该提供项目名称文件夹,然后

.when('/', {templateUrl: '/signup.html',  
controller: 'submitCtrl'
 })

将按预期工作