angularjs路由傳值$routeParams詳解
AngularJS利用路由傳值,供大家參考,具體內(nèi)容如下
1.導(dǎo)包
<script src="angular.min.js"></script> <script src="angular-route.js"></script>
2.依賴注入ngRoute
var myapp=angular.module("myapp",["ngRoute"]);
3.配置路由
myapp.config(function ($routeProvider) {
//頁面選擇
$routeProvider.when("/home",{
// template:"<h2>這是主頁面</h2>"
templateUrl:"home.html"
}).when("/about",{
// template:"<h2>這是關(guān)于我們的信息</h2>"
templateUrl:"about.html"
}).when("/cel",{
// template:"<h2>請(qǐng)聯(lián)系我們</h2>"
templateUrl:"cel.html",
// controller:"celCtrl"
}).when("/cel/:sub",{//傳參數(shù)
templateUrl:"cel.html",
controller:"celCtrl"
})
});
4.寫cel的控制器
myapp.controller("celCtrl",function ($scope,$routeParams) {
//根據(jù)傳過來的參數(shù)給輸入框賦值
var param = $routeParams["sub"];
if(param=="a"){
$scope.mname="我想提建議...";
}else if(param=="b"){
$scope.mname="我想購(gòu)買..."
}
});
5.通過about頁面?zhèn)髦到ocel頁面
<p>About頁面</p> <ul> <li><a href="#cel/a" rel="external nofollow" >告訴我們</a></li> <li><a href="#cel/b" rel="external nofollow" >詢價(jià)</a></li> </ul>
修改cel頁面的輸入框的值
<p>顯示頁面</p> <ul> <li><input type="text" placeholder="sub" ng-model="mname"></li> <li><input type="text" placeholder="Message" ng-model="minfo"></li> </ul>
最后是頁面布局
<body ng-app="myapp"> <!--頁面布局--> <header> <p>我的站點(diǎn)</p> <div> <a href="#home" rel="external nofollow" >主頁</a> <a href="#about" rel="external nofollow" >關(guān)于我們</a> <a href="#cel" rel="external nofollow" >聯(lián)系我們</a> </div> </header> <div ng-view="" class="View"></div> </body>
看看效果:




以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular中ng?update命令force參數(shù)含義詳解
這篇文章主要為大家介紹了Angular中ng?update命令force參數(shù)含義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
AngularJS 入門教程之HTML DOM實(shí)例詳解
本文主要介紹AngularJS HTML DOM,這里幫大家整理了詳細(xì)的資料,并附實(shí)例代碼詳細(xì)講解,有需要的小伙伴可以參考下2016-07-07
詳解Angular Karma測(cè)試的持續(xù)集成實(shí)踐
這篇文章主要介紹了詳解Angular Karma測(cè)試的持續(xù)集成實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
AngulaJS路由 ui-router 傳參實(shí)例
本篇文章主要介紹了AngulaJS路由 ui-router 傳參實(shí)例 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
ionic4+angular7+cordova上傳圖片功能的實(shí)例代碼
ionic是一個(gè)垮平臺(tái)開發(fā)框架,可通過web技術(shù)開發(fā)出多平臺(tái)的應(yīng)用。這篇文章主要介紹了ionic4+angular7+cordova上傳圖片功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06

