三種AngularJS中獲取數(shù)據(jù)源的方式
在AngularJS中,可以從$rootScope中獲取數(shù)據(jù)源,也可以把獲取數(shù)據(jù)的邏輯封裝在service中,然后注入到app.run函數(shù)中,或者注入到controller中。本篇就來(lái)整理獲取數(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中的某個(gè)字段中,很容易被重寫(xiě)。
■ 數(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中似乎這樣寫(xiě)比較好:
<input type="submit" ng-click=""TodoService.todos.addodo(newTodo) />
在service中增加一個(gè)方法:
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;
})
在對(duì)應(yīng)的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中,與服務(wù)端交互
在實(shí)際項(xiàng)目中,service還需要和服務(wù)端交互。
var app = angular.module("app",[]);
app.service("TodoService", function($q, $timeout){
this.getTodos = function(){
var d = $q.defer();
//模擬一個(gè)請(qǐng)求
$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ù)源的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。
- AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息
- Angular.JS內(nèi)置服務(wù)$http對(duì)數(shù)據(jù)庫(kù)的增刪改使用教程
- AngularJS實(shí)現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實(shí)例
- 基于AngularJS實(shí)現(xiàn)頁(yè)面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
- Angularjs實(shí)現(xiàn)多個(gè)頁(yè)面共享數(shù)據(jù)的方式
- 詳解angularJs中自定義directive的數(shù)據(jù)交互
- Angular.js如何從PHP讀取后臺(tái)數(shù)據(jù)
- Angularjs 滾動(dòng)加載更多數(shù)據(jù)
- 深入學(xué)習(xí)AngularJS中數(shù)據(jù)的雙向綁定機(jī)制
- 在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的步驟詳解
- Angular.JS讀取數(shù)據(jù)庫(kù)數(shù)據(jù)調(diào)用完整實(shí)例
相關(guān)文章
Angular.JS學(xué)習(xí)之依賴注入$injector詳析
隨著javaEE的spring框架的興起,依賴注入(IoC)的概念徹底深入人心,它徹底改變了我們的編碼模式和思維。在AngularJS中也有依賴注入的概念,像spring中的依賴注入,但是又有所不同。Angular中只需要在需要的地方聲明一下即可,類(lèi)似模塊的引用,因此十分方便。2016-10-10
angular-cli修改端口號(hào)【angular2】
本篇文章主要介紹了angular2中angular-cli修改端口號(hào)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
詳細(xì)談?wù)凙ngularJS的子級(jí)作用域問(wèn)題
大家在使用angularjs的時(shí)候,很容易忽略AngularJS自帶指令的作用域問(wèn)題,有一些指令會(huì)產(chǎn)生獨(dú)立的自己作用域,造成子級(jí)無(wú)法與父級(jí)作用域雙向綁定的問(wèn)題。下面我們來(lái)看看這些問(wèn)題,有需要的可以參考借鑒。2016-09-09
angular中實(shí)現(xiàn)控制器之間傳遞參數(shù)的方式
本篇文章主要介紹了angular中實(shí)現(xiàn)控制器之間傳遞參數(shù)的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
解決angularjs WdatePicker ng-model的問(wèn)題
今天小編就為大家分享一篇解決angularjs WdatePicker ng-model的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
AngularJS ui-router (嵌套路由)實(shí)例
本篇文章主要介紹了AngularJS ui-router (嵌套路由)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Angular Universal服務(wù)器端渲染避免 window is not&
這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯(cuò)誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
簡(jiǎn)介AngularJS的視圖功能應(yīng)用
這篇文章主要介紹了AngularJS的視圖功能應(yīng)用,包括ng-view和ng-template以及$routeProvider的使用,以及 $routeProvider 需要的朋友可以參考下2015-06-06

