第5讲 如何在项目中使用全局配置?
Q:ionic开发,说纯粹一点,用的就是html+css+js,那么无疑跟web开发的方式是类似的。在这里给大家分享一个小技巧,如何在项目中使用全局配置?
A:我的做法是,同web开发一样,在www/js文件夹中新建一个js文件,命名为config.js,在index.html中引用该config.js,就可以在controllers.js中对config.js里申明的对象、变量、函数等进行调用。
上一讲中的imgUrl就是声明在config.js中的:
var domain = "http://www.tngou.net/api"
var imgUrl = "http://tnfs.tngou.net/image"
var cache ={
user :"TongeNews"
}
3.在controllers.js中的调用
.controller('Tab1Ctrl', function ($scope,$rootScope, Tab1Service, $ionicSlideBoxDelegate, $ionicTabsDelegate) {
$rootScope.imgUrl = imgUrl;
})
4.在视图中可直接这样调用
<div class="list">
<a ng-repeat="item in items" class="item item-thumbnail-right item-text-wrap" href="#">
<img ng-src="{{imgUrl+item.img}}" width="30" height="30" alt="">
<h3>{{::item.title}}</h3>
<p>{{::item.description | substring:item.description}}</p>
</a>
</div>
完!