欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

三種AngularJS中獲取數(shù)據(jù)源的方式

 更新時間:2016年02月02日 08:58:53   作者:Darren Ji  
這篇文章主要介紹了三種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;
})
 

在對應(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ù)端交互

在實際項目中,service還需要和服務(wù)端交互。

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ù)源的方法,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • angularjs表格分頁功能詳解

    angularjs表格分頁功能詳解

    本文給大家分享的是個人在項目中使用angularjs實現(xiàn)表格分頁功能的思路和代碼,非常的簡單實用,有需要的小伙伴可以參考下。
    2016-01-01
  • Angular.JS學(xué)習(xí)之依賴注入$injector詳析

    Angular.JS學(xué)習(xí)之依賴注入$injector詳析

    隨著javaEE的spring框架的興起,依賴注入(IoC)的概念徹底深入人心,它徹底改變了我們的編碼模式和思維。在AngularJS中也有依賴注入的概念,像spring中的依賴注入,但是又有所不同。Angular中只需要在需要的地方聲明一下即可,類似模塊的引用,因此十分方便。
    2016-10-10
  • angular-cli修改端口號【angular2】

    angular-cli修改端口號【angular2】

    本篇文章主要介紹了angular2中angular-cli修改端口號的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • 詳細談?wù)凙ngularJS的子級作用域問題

    詳細談?wù)凙ngularJS的子級作用域問題

    大家在使用angularjs的時候,很容易忽略AngularJS自帶指令的作用域問題,有一些指令會產(chǎn)生獨立的自己作用域,造成子級無法與父級作用域雙向綁定的問題。下面我們來看看這些問題,有需要的可以參考借鑒。
    2016-09-09
  • angular中實現(xiàn)控制器之間傳遞參數(shù)的方式

    angular中實現(xiàn)控制器之間傳遞參數(shù)的方式

    本篇文章主要介紹了angular中實現(xiàn)控制器之間傳遞參數(shù)的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • AngularJS基礎(chǔ)知識

    AngularJS基礎(chǔ)知識

    這篇文章主要介紹了AngularJS基礎(chǔ)知識,包括AngularJS定義和特點以及構(gòu)建AngularJS應(yīng)用的方法,推薦給大家。
    2014-12-12
  • 解決angularjs WdatePicker ng-model的問題

    解決angularjs WdatePicker ng-model的問題

    今天小編就為大家分享一篇解決angularjs WdatePicker ng-model的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • AngularJS ui-router (嵌套路由)實例

    AngularJS ui-router (嵌套路由)實例

    本篇文章主要介紹了AngularJS ui-router (嵌套路由)實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Angular Universal服務(wù)器端渲染避免 window is not defined錯誤消息

    Angular Universal服務(wù)器端渲染避免 window is not&

    這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 簡介AngularJS的視圖功能應(yīng)用

    簡介AngularJS的視圖功能應(yīng)用

    這篇文章主要介紹了AngularJS的視圖功能應(yīng)用,包括ng-view和ng-template以及$routeProvider的使用,以及 $routeProvider 需要的朋友可以參考下
    2015-06-06

最新評論