三種AngularJS中獲取數(shù)據(jù)源的方式
在AngularJS中,可以從$rootScope中獲取數(shù)據(jù)源,也可以把獲取數(shù)據(jù)的邏輯封裝在service中,然后注入到app.run函數(shù)中,或者注入到controller中。本篇就來整理獲取數(shù)據(jù)的幾種方式。
■ 數(shù)據(jù)源放在$rootScope中
var app = angular.module("app",[]);
app.run(function($rootScope){
$rootScope.todos = [
{item:"",done:true},
{item:"",done:false}
];
})
<div ng-repeat="todo in todos">
{{todo.item}}
</div>
<form>
<input type="text" ng-model="newTodo" />
<input type="submit" ng-click=""todos.push({item:newTodo, done:false}) />
</form>
以上,把數(shù)據(jù)源放在$rootScope中的某個字段中,很容易被重寫。
■ 數(shù)據(jù)源放在service中,把servie注入到run函數(shù)中
app.service("TodoService", function(){
this.todos = [
{item:"",done:true},
{item:"",done:false}
];
})
app.run(function($rootScope, TodoService){
$rootScope.TodoService = TodoService;
})
<div ng-repeat="todo in TodoService.todos">
{{todo.item}}
</div>
<form>
<input type="text" ng-model="newTodo" />
<input type="submit" ng-click=""TodoService.todos.push({item:newTodo, done:false}) />
</form>
在html中似乎這樣寫比較好:
<input type="submit" ng-click=""TodoService.todos.addodo(newTodo) />
在service中增加一個方法:
app.service("TodoService", function(){
this.todos = [
{item:"",done:true},
{item:"",done:false}
];
this.addTodo = fucntion(newTodo){
this.todos.push({item:newTodo, done:false})
}
})
■ 數(shù)據(jù)源放在service中,把servie注入到controller中
app.controller("TodoCtrl", function($scope, TodoService){
this.TodoService = TodoServce;
})
在對應的html中:
<body ng-app="app" ng-controller="TodoCtrl as todoCtrl">
<div ng-repeat="todo in todoCtrl.TodoService.todos">
{{todo.item}}
</div>
</body>
<form>
<input type="text" ng-model="newTodo" />
<input type="submit" ng-click="todoCtrl.TodoService.addTodo(newTodo)"/>
</form>
■ 數(shù)據(jù)源放在service中,把servie注入到controller中,與服務端交互
在實際項目中,service還需要和服務端交互。
var app = angular.module("app",[]);
app.service("TodoService", function($q, $timeout){
this.getTodos = function(){
var d = $q.defer();
//模擬一個請求
$timeout(function(){
d.resolve([
{item:"", done:false},
...
])
},3000);
return d.promise;
}
this.addTodo = function(item){
this.todos.push({item:item, done:false});
}
})
app.controller("TodoCtrl", function(TodoService){
var todoCtrl = this;
TodoService.getTodos().then(function(result){
todoCtrl.todos = result;
})
todoCtrl.addTodo = TodoService.addTodo;
})
以上就是AngularJS中獲取數(shù)據(jù)源的方法,希望對大家的學習有所幫助。
- AngularJS實現(xiàn)分頁顯示數(shù)據(jù)庫信息
- Angular.JS內置服務$http對數(shù)據(jù)庫的增刪改使用教程
- AngularJS實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實例
- 基于AngularJS實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- Angularjs實現(xiàn)多個頁面共享數(shù)據(jù)的方式
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- Angular.js如何從PHP讀取后臺數(shù)據(jù)
- Angularjs 滾動加載更多數(shù)據(jù)
- 深入學習AngularJS中數(shù)據(jù)的雙向綁定機制
- 在 Angular6 中使用 HTTP 請求服務端數(shù)據(jù)的步驟詳解
- Angular.JS讀取數(shù)據(jù)庫數(shù)據(jù)調用完整實例
相關文章
詳解Angular CLI + Electron 開發(fā)環(huán)境搭建
本篇文章主要介紹了Angular CLI + Electron 開發(fā)環(huán)境搭建,具有一定的參考價值,有興趣的可以了解一下2017-07-07
Angular通過angular-cli來搭建web前端項目的方法
這篇文章主要介紹了Angular通過angular-cli來搭建web前端項目的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
angular源碼學習第一篇 setupModuleLoader方法
這篇文章主要介紹了angular源碼學習第一篇,setupModuleLoader方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Angular 4 依賴注入學習教程之FactoryProvider的使用(四)
這篇文章主要給大家介紹了關于Angular 4 依賴注入之FactoryProvider使用的相關資料,文中介紹的非常詳細,對大家學習或者使用Angular4具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-06-06
AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法詳解
這篇文章主要介紹了AngularJS使用ng-repeat遍歷二維數(shù)組元素的方法,結合實例形式分析了AngularJS二維數(shù)組元素遍歷的相關操作技巧,需要的朋友可以參考下2017-11-11
AngularJS實現(xiàn)動態(tài)添加Option的方法
這篇文章主要介紹了AngularJS實現(xiàn)動態(tài)添加Option的方法,涉及AngularJS事件響應及頁面元素動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下2017-05-05
詳解Angular路由 ng-route和ui-router的區(qū)別
這篇文章主要介紹了詳解Angular路由 ng-route和ui-router的區(qū)別,分別介紹了兩種路由的用法和區(qū)別,有興趣的可以了解一下2017-05-05
Angular2中constructor和ngOninit的使用講解
這篇文章主要介紹了Angular2中constructor和ngOninit的使用講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

