最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

angularJs在多個(gè)控制器中共享服務(wù)數(shù)據(jù)的方法

 更新時(shí)間:2018年09月30日 10:50:03   作者:泠泠在路上  
今天小編就為大家分享一篇angularJs在多個(gè)控制器中共享服務(wù)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

如下所示:

<div ng-app="module">
 <div ng-controller="ctrl1">
  <table border="1" width="600">
   <tr>
    <td>網(wǎng)站名稱</td>
    <td>網(wǎng)址</td>
   </tr>
   <tr ng-repeat="v in data.webs">
    <td>{{v.name}}</td>
    <td>{{v.url}}</td>
   </tr>
  </table>
 </div>
 <hr>
 <div ng-controller="ctrl2">
  <table border="1" width="600">
   <tr>
    <td>網(wǎng)站名稱</td>
    <td>網(wǎng)址</td>
   </tr>
   <tr ng-repeat="v in data.webs">
    <td>{{v.name}}</td>
    <td>{{v.url}}</td>
   </tr>
  </table>
  <h1>{{web.name}}</h1>
  <button ng-click="removeAll()">刪除所有數(shù)據(jù)</button>
 </div>
</div>
<script>
 var m = angular.module('module', []);
 //定義服務(wù)
 m.factory('videoServer', ['$http', function ($http) {
  var obj = {
   data: {webs:[]},
   //所有數(shù)據(jù)
   all: function () {
    return $http({url: '1.php'}).then(function (response) {
     obj.data.webs = response.data;
     return obj.data;
    });
   },
   //獲取一條數(shù)據(jù)
   find: function (id) {
    return this.all().then(function (data) {
     for (var i = 0; i < data.length; i++) {
      if (data[i].id == id) {
       return data[i];
      }
     }
    });
   },
   //刪除所有數(shù)據(jù)
   flush: function () {
    obj.data.webs=[];
   }
  };
  return obj;
 }]);
 //控制器ctrl1
 m.controller('ctrl1', ['$scope', 'videoServer', function ($scope, videoServer) {
  videoServer.all().then(function (data) {
   $scope.data = data;
  });
 }]);

  //控制器ctrl2
 m.controller('ctrl2', ['$scope', 'videoServer', function ($scope, videoServer) {
  videoServer.all().then(function (data) {
   $scope.data = data;
  });
  videoServer.find(1).then(function (data) {
   $scope.web = data;
  })
  $scope.removeAll=function(){
   videoServer.flush();
  }
 }]);
</script>

1.php

<?php
$data = [
 [ 'name' => '百度', 'url' => 'www.baidu.com' ],
 [ 'name' => '谷歌', 'url' => 'google.com' ],
];
echo json_encode($data,JSON_UNESCAPED_UNICODE);

以上這篇angularJs在多個(gè)控制器中共享服務(wù)數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Angular頁面間切換及傳值的4種方法

    Angular頁面間切換及傳值的4種方法

    這篇文章主要為大家詳細(xì)介紹了Angular頁面間切換及傳值的四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Angular中使用ng-zorro圖標(biāo)庫部分圖標(biāo)不能正常顯示問題

    Angular中使用ng-zorro圖標(biāo)庫部分圖標(biāo)不能正常顯示問題

    這篇文章主要介紹了Angular中使用ng-zorro圖標(biāo)庫部分圖標(biāo)不能正常顯示問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁實(shí)例代碼

    AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁實(shí)例代碼

    這篇文章主要介紹了AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁的相關(guān)資料,并附實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下
    2016-10-10
  • Angular使用Md5加密的解決方法

    Angular使用Md5加密的解決方法

    這篇文章主要介紹了Angular使用Md5加密的解決方法,需要的朋友可以參考下
    2017-09-09
  • 最新評(píng)論

    本溪| 丽水市| 齐河县| 舒城县| 缙云县| 镇赉县| 镇江市| 邯郸市| 吴旗县| 游戏| 平顶山市| 威信县| 和田市| 民权县| 鹤山市| 昌平区| 南开区| 汉川市| 手游| 安图县| 白城市| 绥阳县| 鸡西市| 凤山市| 临潭县| 临桂县| 枞阳县| 博野县| 大余县| 德阳市| 疏勒县| 乐亭县| 大丰市| 边坝县| 台东市| 阿鲁科尔沁旗| 威宁| 甘泉县| 滨州市| 三河市| 辽阳市|