【问题标题】:How to redirect Page using Angular Js?如何使用 Angular Js 重定向页面?
【发布时间】:2017-08-18 11:19:20
【问题描述】:

我想知道如何使用 Angular Js 重定向到另一个页面。

我已经在这里关注了几个问题,但没有找到任何成功的答案

这是我的代码:

var app = angular.module('formExample',[]);
app.controller('formCtrl',function($scope,$http){    
    $scope.insertData=function(){      

      //  if($scope.name =='' && $scope.email == '' && $scope.message = '' && $scope.price =='' && $scope.date == null && $scope.client == ''){return;}
        $http.post("/php/login.php", {
           "email": $scope.email, "password": $scope.password
        }).then(function(response, $location){
                alert("Login Successfully");
                $scope.email = '';
                $scope.password = '';
                $location.path('/clients');

            },function(error){
                alert("Sorry! Data Couldn't be inserted!");
                console.error(error);

            });
        }
    });

我收到此错误:

TypeError: Cannot read property 'path' of undefined

【问题讨论】:

  • 你需要在你的控制器中注入$location
  • 详细的答案已经在stackoverflow中提供here
  • 在你的控制器中注入 $location 并且不要忘记从成功回调函数中移除它。

标签: javascript angularjs


【解决方案1】:

您需要将 $location 注入到您的控制器中,

app.controller('formCtrl',function($scope,$http,$location){    

【讨论】:

  • 错误:$injector:unpr 未知提供者 未知提供者:$urlProvider
  • 我在任何地方都看不到 urlProvider
  • 不行,因为路径没有定义@Sajeetharan
  • 什么?没看懂
【解决方案2】:

你可以使用普通的 JS

window.location.href = '/my-other-page'

或者,如果您使用的是“ui-router”

$state.reload()

$state.go($state.current.name, {}, {reload: true})

不要忘记将 $state 注入到您的控制器依赖项中:

app.controller('formCtrl',function($scope, $http, $state){ 

【讨论】:

  • @Maxwelt 未知提供者:$urlProvider
  • 将 '$state' 注入到控制器的依赖项中
  • app.controller('formCtrl',function($scope,$http, $state){
【解决方案3】:

您可以在控制器中使用 $window 进行重定向。

$window.location.href = '/index.html';

希望对你有帮助

【讨论】:

  • 错误:$injector:unpr 未知提供者 未知提供者:$urlProvider
  • $window.location.href 应该只用于整页重新加载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 2016-08-02
  • 2015-06-04
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多