假设你正在使用路由:
// 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中,当单击按钮时,您希望导航到关于页面。一种方法是
<a href="#/about">
... 但是ng-click似乎在这里也很有用。
这个假设正确吗?用ng-click代替anchor?
如果是的话,它是如何运作的呢?即:
<div ng-click=“/about”>
我使用ng-click指令调用一个函数,同时请求route templateUrl,以决定哪个<div>必须显示或隐藏在route templateUrl页面或不同的场景。
手指1.6.9
让我们看一个例子,当在路由页面,我需要添加<div>或编辑<div>,我控制使用父控制器模型$作用域。addProduct和$scope。editProduct布尔。
RoutingTesting.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-route.min.js"></script>
<script>
var app = angular.module("MyApp", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/TestingPage", {
templateUrl: "TestingPage.html"
});
});
app.controller("HomeController", function($scope, $location){
$scope.init = function(){
$scope.addProduct = false;
$scope.editProduct = false;
}
$scope.productOperation = function(operationType, productId){
$scope.addProduct = false;
$scope.editProduct = false;
if(operationType === "add"){
$scope.addProduct = true;
console.log("Add productOperation requested...");
}else if(operationType === "edit"){
$scope.editProduct = true;
console.log("Edit productOperation requested : " + productId);
}
//*************** VERY IMPORTANT NOTE ***************
//comment this $location.path("..."); line, when using <a> anchor tags,
//only useful when <a> below given are commented, and using <input> controls
$location.path("TestingPage");
};
});
</script>
</head>
<body ng-app="MyApp" ng-controller="HomeController">
<div ng-init="init()">
<!-- Either use <a>anchor tag or input type=button -->
<!--<a href="#!TestingPage" ng-click="productOperation('add', -1)">Add Product</a>-->
<!--<br><br>-->
<!--<a href="#!TestingPage" ng-click="productOperation('edit', 10)">Edit Product</a>-->
<input type="button" ng-click="productOperation('add', -1)" value="Add Product"/>
<br><br>
<input type="button" ng-click="productOperation('edit', 10)" value="Edit Product"/>
<pre>addProduct : {{addProduct}}</pre>
<pre>editProduct : {{editProduct}}</pre>
<ng-view></ng-view>
</div>
</body>
</html>
TestingPage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.productOperation{
position:fixed;
top: 50%;
left: 50%;
width:30em;
height:18em;
margin-left: -15em; /*set to a negative number 1/2 of your width*/
margin-top: -9em; /*set to a negative number 1/2 of your height*/
border: 1px solid #ccc;
background: yellow;
}
</style>
</head>
<body>
<div class="productOperation" >
<div ng-show="addProduct">
<h2 >Add Product enabled</h2>
</div>
<div ng-show="editProduct">
<h2>Edit Product enabled</h2>
</div>
</div>
</body>
</html>
两页都是
RoutingTesting.html(父),TestingPage.html(路由页面)在同一目录,
希望这能帮助到一些人。
路由监视$location服务并响应URL的更改(通常通过散列)。要“激活”路由,只需更改URL。最简单的方法就是使用锚标记。
<a href="#/home">Go Home</a>
<a href="#/about">Go to About</a>
不需要更复杂的了。然而,如果你必须从代码中完成,正确的方法是使用$location服务:
$scope.go = function ( path ) {
$location.path( path );
};
例如,一个按钮可以触发:
<button ng-click="go('/home')"></button>