javascript - AngularJs location.path 不传递参数

标签 javascript angularjs ionic-framework

我有登录页面。

Login.html

<ion-view view-title="Login" name="login-view">
  <ion-content class="padding">
      <div class="list list-inset">
          <label class="item item-input">
              <input type="text" placeholder="Username" ng-model="data.username">
          </label>
          <label class="item item-input">
              <input type="password" placeholder="Password" ng-model="data.password">
          </label>
      </div>
      <button class="button button-block button-calm" ng-click="login()">Login</button>
  </ion-content>
</ion-view>

当我点击登录按钮login()方法在下面工作

LoginCtrl Controller

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http) {
  $scope.login = function () {
    $http({
      method: 'GET',
      url: 'http://localhost:49431/api/values/GetUserInfo?username=' + $scope.data.username + '&password=' + $scope.data.password + ''
    }).then(function successCallback(response) {
      if (response.data.Status == true) { // Success
        $location.path('/HomePage/').search({ id: '1' }); // Problem here
      } else { // Fail
        var alertPopup = $ionicPopup.alert({
          title: 'Login failed!',
          template: 'Username or password is incorrect!'
        });
        $scope.data.username = "";
        $scope.data.password = "";
      }
    }, function errorCallback(response) {
      alert("error");
    });
  }
})

我用

$location.path('/HomePage/').search({ id: '1' }); 

代码,以便将 id 参数传递到 HomePage.Html 页面。但是,位置路径既不传递参数,也不重定向页面。很快,位置路径不起作用。

HomePageCtrl Controller

.controller('HomePageCtrl', function ($scope, HomePageService, $state, $location, $cordovaCamera, $stateParams, $routeParams) {
    alert($routeParams.id + $stateParams.id);
}

app.js

.config(function ($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('HomePage', {
      url: '/HomePage',
      templateUrl: 'templates/HomePage.html',
      controller: 'HomePageCtrl'
    })
    .state('login', {
      url: '/login',
      templateUrl: 'templates/Login.html',
      controller: 'LoginCtrl'
    });
  $urlRouterProvider.otherwise('/login');
});

问题:

如何使用 $location.path 将参数从 LoginCtrl Controller 传递到 HomePageCtrl Controller ?

任何帮助将不胜感激。

谢谢。

最佳答案

为什么不使用$state而不是$location

App.js:

 .state('HomePage', {
             url: '/HomePage/:id',
             templateUrl: 'templates/HomePage.html',
             controller: 'HomePageCtrl'
  })

登录Ctrl:

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http) {

           $scope.login = function () {

            $http({
                method: 'GET',
                url: 'http://localhost:49431/api/values/GetUserInfo?username=' + $scope.data.username + '&password=' + $scope.data.password + ''
            }).then(function successCallback(response) {

                if (response.data.Status == true) { // Success

                    $state.go('HomePage',{id:'1'})

                }
                else { // Fail
                    var alertPopup = $ionicPopup.alert({
                        title: 'Login failed!',
                        template: 'Username or password is incorrect!'
                    });

                    $scope.data.username = "";
                    $scope.data.password = "";
                }

            }, function errorCallback(response) {
                alert("error");
            });
    }
    })

HomeCtrl:

.controller('HomePageCtrl', function ($scope, HomePageService, $state, $location, $cordovaCamera, $stateParams, $routeParams) {

    alert($stateParams.id);
}

关于javascript - AngularJs location.path 不传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125093/

相关文章:

javascript - 命名多个迭代计数器的良好做法

javascript - 如何保存 Firebase 中持久存在的地理位置数据?

angular - ngx-datatable 不能在环境上下文中声明访问器

angular - Ionic 3 密码并使用 formBuilder 确认密码验证

javascript - 从jade发送参数到js/node

javascript - 嵌套构造函数的最佳实践

javascript - 使用按钮滚动 slider

javascript - 清除函数内部的 Angular 超时

javascript - Angular : How to get an element by id with an expression from a directive?

android - Cordova Android 应用程序在拍摄照片时因 RecoverableSecurityException 而崩溃