AngulaJS路由 ui-router 傳參

來源:互聯網
上載者:User

標籤:url   control   scope   pre   value   傳參   other   dex   ons   

在這裡分享我做的一個使用ui-router 傳參的小demo

 

1.首先第一步設定入口檔案index.html,注意載入的順序,先載入包,再載入自己寫的控制器。

<!doctype html><html lang="en" ng-app="routerApp"><head>    <meta charset="utf-8">    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">    <title>測試</title>    <!--lib是angular包的檔案夾-->    <script src="lib/jquery/jquery-1.11.3.min.js"></script>    <script src="lib/angular/angular.js"></script>    <script src="lib/angular-ui/angular-ui-router.js"></script>    <!--js控制器的檔案夾-->    <script src="js/app.js"></script>    <script src="js/indexCtrl.js"></script>    <script src="js/resultCtrl.js"></script></head><body><div ui-view></div></body></html>

 

2.app.js檔案,依賴注入,設定路由,此處的路由是使用ui-router路由,這裡簡單的示範了兩個模板之間的傳參,傳遞參數的模板test.html和接收參數的模板result.html

var routerApp = angular.module(‘routerApp‘, [‘ui.router‘]);routerApp.run(function($rootScope, $state, $stateParams) {    $rootScope.$state = $state;    $rootScope.$stateParams = $stateParams;});routerApp.config(function($stateProvider, $urlRouterProvider) {    $urlRouterProvider.otherwise(‘/index‘);    $stateProvider        .state(‘index‘, {//模板的參數            url: ‘/index‘,//url的參數            templateUrl: ‘templates/test.html‘,//模板的位置            controller: ‘MyController‘        })        .state(‘result‘, {            url: ‘/result/:id/:number‘,//需要傳的參數的鍵名            templateUrl: ‘templates/result.html‘,            controller: ‘resultCtrl‘        });});

 

3.第一個首頁面的模板test.html,並且設定點擊事件toResult()

<meta charset="UTF-8"><div>hello world</div><input type="button" ng-click="toResult()" value="toResult">

 

4.test.html的控制器indexCtrl.js,設定需要傳遞的參數$scope.abc和$scope.toResult,點擊事件toResult()裡面其實就是一個$state.go(‘模板的參數‘,{app.js裡面需要傳的參數的鍵名:需要傳的參數值})的方法

routerApp.controller(‘MyController‘, function($scope, $state) {    $scope.abc = "nice";//需要傳的參數值    $scope.def = 10;//需要傳的參數值    $scope.toResult = function(){        $state.go(‘result‘,{id: $scope.abc,number: $scope.def});    }});

 

5.接收參數的模板result.html

<meta charset="UTF-8"><div>hello world2</div>

 

6.result.html的控制器resultCtrl.js,這裡使用$stateParams的方法去接收上一個頁面傳遞過來的參數

routerApp.controller(‘resultCtrl‘, function($scope, $state, $stateParams) {    var id = $stateParams.id;    var number = $stateParams.number;    console.log(id);    console.log(number);});

 

項目目錄

js\app.js、indexCtrl.js、resultCtrl.js

lib\

jquery\jquery-1.11.3.min.js
angular\angular.js
angular-ui\angular-ui-router.js

templates\test.html、result.html

index.html

其實整個過程並不難,只是穿插在模板和控制器之間,容易讓人摸不著頭腦,只要分清楚具體的參數是對應哪一個,很容易理解。

AngulaJS路由 ui-router 傳參

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.