国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

angularJs在多個控制器中共享服務數據的方法

2024-05-06 15:29:09
字體:
來源:轉載
供稿:網友

如下所示:

<div ng-app="module"> <div ng-controller="ctrl1">  <table border="1" width="600">   <tr>    <td>網站名稱</td>    <td>網址</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>網站名稱</td>    <td>網址</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()">刪除所有數據</button> </div></div><script> var m = angular.module('module', []); //定義服務 m.factory('videoServer', ['$http', function ($http) {  var obj = {   data: {webs:[]},   //所有數據   all: function () {    return $http({url: '1.php'}).then(function (response) {     obj.data.webs = response.data;     return obj.data;    });   },   //獲取一條數據   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];      }     }    });   },   //刪除所有數據   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在多個控制器中共享服務數據的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 弋阳县| 静海县| 壶关县| 楚雄市| 乡城县| 盱眙县| 华蓥市| 陵水| 泰兴市| 兴化市| 峨边| 共和县| 沿河| 密云县| 佛冈县| 涡阳县| 巴林左旗| 阿克| 库车县| 抚宁县| 灌阳县| 贡山| 保德县| 扶余县| 呼和浩特市| 密云县| 淮安市| 克什克腾旗| 运城市| 贡觉县| 信阳市| 广宁县| 新沂市| 涟源市| 鄂托克旗| 鸡西市| 开封市| 洪洞县| 辽阳市| 澄城县| 华蓥市|