您好,登錄后才能下訂單哦!
AngulagJs的頁(yè)面使用Route跳轉(zhuǎn)
1.除了引用AngularJs.js外,還要引用路由JS, "~/Scripts/angularjs/angular-route.js"
2.通過$routeProvider定義路由,示例
var testModule = angular.module('testModule', ['ngRoute']); testModule.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/2', {//'/2'定義的路由路徑,以后通過此路徑訪問,通常定義為短路徑 templateUrl: "/home/index2",//"/home/index2"是路由實(shí)際訪問的路徑,可以是asp.net mvc的訪問路徑(如此例),也可是具體的頁(yè)面路徑,如“test/test.html" controller:'testController'//路由跳轉(zhuǎn)的controller,后面必須定義此控制器 }); $routeProvider.when('/3', { templateUrl: "/home/index3", controller:'testController' }) }]);
3.使用路由跳轉(zhuǎn),結(jié)合ng-view做spa
3.1 在JS中使用$location進(jìn)行跳轉(zhuǎn),如示例,在需要的時(shí)候調(diào)用goToIndex2即可
testModule.controller("testController", ["$scope", "$location", function ($scope, $location) { $scope.goToIndex2 = function () { $location.path("/2") } }]);
3.2 在html代碼中使用href="#path"來進(jìn)行跳轉(zhuǎn)
<html > <head> <meta name="viewport" content="width=device-width" /> <title>Index1</title> @Styles.Render("~/Content/css/base") @Scripts.Render("~/script/base") <script src="~/scripts/ngmoudle/app.js"></script> </head> <body> <div ng-app="testModule" ng-controller="testController"> <header> <h2>This is Index1</h2> <button type="button" class="btn btn-default" ng-click="goToIndex2()">Index2</button> <a href="#/3" class="btn btn-default">Index3</a><!--通過heft="#path"方式進(jìn)行跳轉(zhuǎn)--> <a href="#/2" class="btn btn-default" >Index2</a> </header> <div ng-view> </div> <footer>PAGE FOOTER</footer> </div> </body> </html>
4.關(guān)于Angularjs版本不得不說的問題(追加部分),“/"變”%2F”問題
新的項(xiàng)目直接使用Nuget獲取Angularjs后,發(fā)現(xiàn)按照以上的寫法,不能跳轉(zhuǎn)了,表現(xiàn)癥狀為 <a href="#/2">Index2</a> 點(diǎn)擊之后,發(fā)現(xiàn)瀏覽器地址變?yōu)椤?%22”,“/"變”%2F”導(dǎo)致路由不能跳轉(zhuǎn)了,一頓猛查和調(diào)試,才發(fā)現(xiàn)AngularJs自1.6版本后對(duì)地址做了特別處理 知道原因后,解決問題也很簡(jiǎn)單,在Angular中聲明用回舊有方式即可。
可參見 http://stackoverflow.com/questions/41211875/angularjs-1-6-0-latest-now-routes-not-working
testModule.config(['$locationProvider', function($locationProvider) { $locationProvider.hashPrefix(''); }]); testModule.config(['$locationProvider', function($locationProvider) { $locationProvider.hashPrefix(''); }]);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。