Javascript 如何在AngularJS中从$scope更改URL?
我使用Javascript 如何在AngularJS中从$scope更改URL?,javascript,angularjs,angularjs-scope,angularjs-routing,angularjs-ng-click,Javascript,Angularjs,Angularjs Scope,Angularjs Routing,Angularjs Ng Click,我使用$scope进行了访问导航,并根据单击使用了它们。我无法在变量中设置URL。正确的URL是http://www.example.com/base/index或http://www.example.com/base/thread,或http://www.example.com/base/tag。 但结果总是`甚至想到,我点击了另一个导航 angular.module('FunnyAnt.Examples.Routing') .config(function ($routeProvider)
$scope
进行了访问导航,并根据单击使用了它们。我无法在变量中设置URL。正确的URL是http://www.example.com/base/index
或http://www.example.com/base/thread
,或http://www.example.com/base/tag
。
但结果总是`甚至想到,我点击了另一个导航
angular.module('FunnyAnt.Examples.Routing')
.config(function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'embedded.home.html',
controller: 'HomeController'
}).
when('/about', {
templateUrl: 'embedded.about.html',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
});
搜索问题后,答案是make$routeProvider
和$location
。我可以使用$scope
并在$scope内设置URL吗
这是我的代码:
$scope.states = {};
$scope.states.activeItem = 'nav1';
$scope.items = [{
id: 'nav1',
target: 'home',
title: 'Home',
icon: 'fa fa-home fa-3x'
}, {
id: 'nav2',
target: 'thread',
title: 'Thread + Answer',
icon: 'fa fa-briefcase fa-3x'
}, {
id: 'nav3',
target: 'tag',
title: 'Tag',
icon: 'fa fa-briefcase fa-3x'
},
{
id: 'nav4',
target: 'trending_tag',
title: 'Trending Tag',
icon: 'fa fa-briefcase fa-3x'
},
{
id: 'nav5',
target: 'category',
title: 'Category',
icon: 'fa fa-briefcase fa-3x'
},
{
id: 'nav6',
target: 'user',
title: 'User',
icon: 'fa fa-user-circle-o fa-3x'
}];
$scope.callToAction = function(actionName){
if($scope[actionName]){
$scope[actionName]();
}else{
alert("YOUR PAGE NOT FOUND BECAUSE AKU BELUM BUAT PAGE NYA HEHE");
}
};
顺便说一下,
$scope.items
执行我的导航 您可以使用更改页面的URL
angular.module('FunnyAnt.Examples.Routing')
.config(function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'embedded.home.html',
controller: 'HomeController'
}).
when('/about', {
templateUrl: 'embedded.about.html',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
});
$location.url('/yourUrl')
将转到/yourUrl
。可以将字符串作为变量传递给函数
请注意,您还可以通过以下方式访问当前URL:
$scope.currentUrl = $location.url(); // <-- No params
$scope.currentUrl=$location.url();// 您可以使用$routeProvider
进行导航
angular.module('FunnyAnt.Examples.Routing')
.config(function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'embedded.home.html',
controller: 'HomeController'
}).
when('/about', {
templateUrl: 'embedded.about.html',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
});
正在工作是否可以将$location.url放入$scope.items中?我的意思是像{id:'nav1',$location.url();}那样的是导航条吗?如果需要,您可以在ng单击中调用$location.url('nav1')
。对导航栏来说是。但它不起作用。URL是正确的,但我的页面没有显示我没有使用routeProvider。就你看来,你想创建一个代码来在菜单或其他东西中显示活动URL,对吗?是的,你是对的。但如果用这种方式设置url,我会感到困惑。