如何/何时使用ng-click呼叫路线? [英] How/when to use ng-click to call a route?
问题描述
假设您正在使用路由:
// bootstrap
myApp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeCtrl'
});
$routeProvider.when('/about', {
templateUrl: 'partials/about.html',
controller: 'AboutCtrl'
});
...
在您的html中,想要在单击按钮时导航到关于页面。一种方法是
And in your html, you want to navigate to the about page when a button is clicked. One way would be
<a href="#/about">
...但是在这里ng-click也很有用。
... but it seems ng-click would be useful here too.
- 该假设正确吗?使用ng-click代替锚点吗?
- 如果这样,会如何IE:
< div ng-click = / about>
推荐答案
路由监视 $ location
服务并响应URL的更改(通常通过哈希值)。要激活路由,只需更改URL。最简单的方法是使用锚标记。
Routes monitor the $location
service and respond to changes in URL (typically through the hash). To "activate" a route, you simply change the URL. The easiest way to do that is with anchor tags.
<a href="#/home">Go Home</a>
<a href="#/about">Go to About</a>
没有什么更复杂的了。但是,如果必须通过代码执行此操作,则正确的方法是使用 $ location
服务:
Nothing more complicated is needed. If, however, you must do this from code, the proper way is by using the $location
service:
$scope.go = function ( path ) {
$location.path( path );
};
例如,按钮可以触发:
<button ng-click="go('/home')"></button>
这篇关于如何/何时使用ng-click呼叫路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!