歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在 AngularJS 應用中通過 JSON 文件來設置狀態

在 AngularJS 應用中通過 JSON 文件來設置狀態

日期:2017/3/1 9:42:17   编辑:Linux編程

想象一個使用簡單的angular UI路由的 angularjs 應用:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="js/app.js"></script>

</head>
<body ng-app="App" ng-controller="MainController">

<div ui-view></div>
</body>
</html>

angular.module('App', ['ui.router'])

.config(function ($stateProvider, $urlRouterProvider, routerProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'templates/home.html'
});

$urlRouterProvider.otherwise('/home');
})

.controller('MainController', function ($scope, router) {
$scope.reload = function() {
router.setUpRoutes();
};
})
;

我們僅定義了一個稱為 'home'的狀態。如果我們需要更多的狀態,只需要在 config() 增加更多的function即可。在這篇文章中,我們將會使用JSON文件方式添加更多的狀態,而不是在代碼中去寫死。

Linux下JSON庫的編譯及代碼測試 http://www.linuxidc.com/Linux/2013-03/81607.htm

jQuery 獲取JSON數據[$.getJSON方法] http://www.linuxidc.com/Linux/2013-03/81673.htm

用jQuery以及JSON包將表單數據轉為JSON字符串 http://www.linuxidc.com/Linux/2013-01/77560.htm

下面是我們在JSON中定義的狀態:

{
"xxx": {
"url": "/xxx",
"templateUrl": "templates/xxx.html"
},

"yyy": {
"url": "/yyy",
"templateUrl": "templates/yyy.html"
},

"zzz": {
"url": "/zzz",
"templateUrl": "templates/zzz.html"
}
}

現在我們的應用變成這樣了:

angular.module('App', ['ui.router', 'Routing'])

.config(function ($stateProvider, $urlRouterProvider, routerProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'templates/home.html'
});

$urlRouterProvider.otherwise('/home');

routerProvider.setCollectionUrl('js/routeCollection.json');
})

.controller('MainController', function ($scope, router) {
$scope.reload = function() {
router.setUpRoutes();
};
})
;

我們可以看到現在正在使用 'Routing'

angular.module('Routing', ['ui.router'])
.provider('router', function ($stateProvider) {

var urlCollection;

this.$get = function ($http, $state) {
return {
setUpRoutes: function () {
$http.get(urlCollection).success(function (collection) {
for (var routeName in collection) {
if (!$state.get(routeName)) {
$stateProvider.state(routeName, collection[routeName]);
}
}
});
}
}
};

this.setCollectionUrl = function (url) {
urlCollection = url;
}
})

.run(function (router) {
router.setUpRoutes();
});

'Routing' 提供了一個叫做 'router' 的provider方法可以獲取到JSON文件並構建各種狀態。

這是一個設想的證明過程。

還有一些問題 (如果你知道怎麼解決請告訴我):

  • 直到我們從一個http請求加載了各種狀態為止, angular 應用在加載的時候沒有得到所有的狀態, 所以我們要使用老的方式至少加載第一個狀態。

  • 我們可以在應用運行的時候重新加載狀態。我們可以新加狀態,但是我們沒法改變已經存的的狀態。

你可以在我的 github 帳戶上看例子。

帶你走近AngularJS系列

  1. 帶你走近AngularJS - 基本功能介紹 http://www.linuxidc.com/Linux/2014-05/102140.htm
  2. 帶你走近AngularJS - 體驗指令實例 http://www.linuxidc.com/Linux/2014-05/102141.htm
  3. 帶你走近AngularJS - 創建自定義指令 http://www.linuxidc.com/Linux/2014-05/102142.htm

如何在 AngularJS 中對控制器進行單元測試 http://www.linuxidc.com/Linux/2013-12/94166.htm

AngularJS 之 Factory vs Service vs Provider http://www.linuxidc.com/Linux/2014-05/101475.htm

AngularJS 的詳細介紹:請點這裡
AngularJS 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved