Far*_*san 161 javascript angularjs
我正在使用ajax调用来执行服务文件中的功能,如果响应成功,我想将页面重定向到另一个URL.目前,我通过使用简单的js"window.location = response ['message'];"来做到这一点.但我需要用angularjs代码替换它.我在stackoverflow上看了各种解决方案,他们使用了$ location.但我是棱角分明并且很难实现它.
$http({
url: RootURL+'app-code/common.service.php',
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
dataType: 'json',
data:data + '&method=signin'
}).success(function (response) {
console.log(response);
if (response['code'] == '420') {
$scope.message = response['message'];
$scope.loginPassword = '';
}
else if (response['code'] != '200'){
$scope.message = response['message'];
$scope.loginPassword = '';
}
else {
window.location = response['message'];
}
// $scope.users = data.users; // assign $scope.persons here as promise is resolved here
})
Run Code Online (Sandbox Code Playgroud)
Ewa*_*ger 219
你可以使用Angular $window
:
$window.location.href = '/index.html';
Run Code Online (Sandbox Code Playgroud)
控制器中的示例用法:
(function () {
'use strict';
angular
.module('app')
.controller('LoginCtrl', LoginCtrl);
LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];
function LoginCtrl($window, loginSrv, notify) {
/* jshint validthis:true */
var vm = this;
vm.validateUser = function () {
loginSrv.validateLogin(vm.username, vm.password).then(function (data) {
if (data.isValidUser) {
$window.location.href = '/index.html';
}
else
alert('Login incorrect');
});
}
}
})();
Run Code Online (Sandbox Code Playgroud)
Cri*_*anu 119
您可以通过不同方式重定向到新URL.
$location.path(YOUR_URL);
或$location.url(YOUR_URL);
.因此,两种方法之间的基本区别在于,它$location.url()
也会影响get参数$location.path()
.我建议阅读文档$location
,$window
以便更好地掌握它们之间的差异.
San*_*tra 12
我使用以下代码重定向到新页面
$window.location.href = '/foldername/page.html';
Run Code Online (Sandbox Code Playgroud)
并在我的控制器函数中注入$ window对象.
Ani*_*ngh 11
它可能会帮助你!!
AngularJs代码示例
var app = angular.module('app', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
// For any unmatched url, send to /index
$urlRouterProvider.otherwise("/login");
$stateProvider
.state('login', {
url: "/login",
templateUrl: "login.html",
controller: "LoginCheckController"
})
.state('SuccessPage', {
url: "/SuccessPage",
templateUrl: "SuccessPage.html",
//controller: "LoginCheckController"
});
});
app.controller('LoginCheckController', ['$scope', '$location', LoginCheckController]);
function LoginCheckController($scope, $location) {
$scope.users = [{
UserName: 'chandra',
Password: 'hello'
}, {
UserName: 'Harish',
Password: 'hi'
}, {
UserName: 'Chinthu',
Password: 'hi'
}];
$scope.LoginCheck = function() {
$location.path("SuccessPage");
};
$scope.go = function(path) {
$location.path("/SuccessPage");
};
}
Run Code Online (Sandbox Code Playgroud)
在 AngularJS 中,您可以使用如下方式将您的表单(提交时)重定向到其他页面window.location.href='';
:
postData(email){
if (email=='undefined') {
this.Utils.showToast('Invalid Email');
} else {
var origin = 'Dubai';
this.download.postEmail(email, origin).then(data => {
...
});
window.location.href = "https://www.thesoftdesign.com/";
}
}
Run Code Online (Sandbox Code Playgroud)
简单地试试这个:
window.location.href = "https://www.thesoftdesign.com/";
Run Code Online (Sandbox Code Playgroud)
我在 Angular 应用程序中重定向到不同页面时也遇到了问题
您可以添加$window
Ewald 在他的回答中建议的,或者如果您不想添加$window
,只需添加超时即可!
setTimeout(function () {
window.location.href = "http://whereeveryouwant.com";
}, 500);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
429488 次 |
最近记录: |