Javascript $location.path(“/”);工作不正常
当我尝试使用$location.path()时,url会发生更改,但我传入的任何内容都会显示在我的当前url上。我希望在执行addShow()时,转到index.html,这是我的主页。你能帮忙吗。 url更改如下: 当我执行addShow函数时,它会变为: 我想把它改成我的主页Javascript $location.path(“/”);工作不正常,javascript,angularjs,Javascript,Angularjs,当我尝试使用$location.path()时,url会发生更改,但我传入的任何内容都会显示在我的当前url上。我希望在执行addShow()时,转到index.html,这是我的主页。你能帮忙吗。 url更改如下: 当我执行addShow函数时,它会变为: 我想把它改成我的主页 angular.module('app').controller("MainController",['$location', function($location) { var vm = this;
angular.module('app').controller("MainController",['$location', function($location) {
var vm = this;
vm.title = 'TV Show';
vm.searchInput = '';
vm.shows = [{
title: 'Game of Thrones',
year: 2011,
favorite: true
}, {
title: 'Walking Dead',
year: 2010,
favorite: false
}, {
title: 'Firefly',
year: 2002,
favorite: true
}, {
title: 'Banshee',
year: 2013,
favorite: true
}, {
title: 'Greys Anatomy',
year: 2005,
favorite: false
}];
vm.orders = [{
id: 1,
title: 'Year Ascending',
key: 'year',
reverse: false
}, {
id: 2,
title: 'Year Descending',
key: 'year',
reverse: true
}, {
id: 3,
title: 'Title Ascending',
key: 'title',
reverse: false
}, {
id: 4,
title: 'Title Descending',
key: 'title',
reverse: true
}];
vm.order = vm.orders[0];
vm.new = {};
vm.addShow = function() {
vm.shows.push(vm.new);
console.log(vm.shows);
vm.new = {};
$location.path("/");
};
}]);
我的配置功能如下:
(function() {
"use strict";
angular
.module("app")
.config(function($routeProvider, $locationProvider) {
//$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: 'list.html',
controller: 'MainController',
controllerAs: 'main',
})
.when('/add', {
templateUrl: 'add.html',
controller: 'MainController',
controllerAs: 'main',
})
;
});
})();
尝试替换
$location.path(“/”)代码>带有
$window.location.href=“/”代码>您需要为您的应用程序提供一个url,因为当您调用path方法时,会将“”视为应用程序的基本url,因此不正确
在主html文件的头部指定url基
<base href="/">
示例:只要使用controllerAs:'vm',我认为它会起作用。只需回复我我查看了您的Plunkr(plnkr.co/edit/0z6ng0?p=preview),问题是您导航到添加页面的方式。此行-a href=“./add.html”直接将页面重定向到add.html。而是使用控制器中的函数将位置更改为“/添加”。
main.ctrl.js(控制器):(添加此函数)
List.html:
你好。。。它不起作用。我的项目在plunkr上。链接是可以请你检查这里。我得到这个错误。{“statusCode”:400,“error”:“Bad Request”,“message”:“child\”previewId\“失败,因为[previewId是必需的]”,“validation”:{“source”:“params”,“keys”:[“previewId”]}}我们需要将此基本href添加到哪里。我尝试在主页中添加,但出现以下错误。未捕获错误:[$injector:modulerr]未能实例化模块应用程序,原因是:错误:[$injector:nomod]模块“app”不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。
$locationProvider.html5Mode(true);
vm.newShow = function() {
$location.path('/add');
};
<a href="">
<button class="btn-primary pull-right" ng-click="main.newShow()">New Show</button>
</a>