對angularJs中2種自定義服務(wù)的實例講解
本篇文章介紹2種自定義服務(wù)的方式,一種是用factory,一種是用service
一、首先介紹使用factory來進行自定義服務(wù)
1.html
<div ng-app="module" ng-controller="ctrl"> <table border="1" width="600"> <tr> <td>網(wǎng)站名稱</td> <td>網(wǎng)址</td> </tr> <tr ng-repeat="v in data"> <td>{{v.name}}</td> <td>{{v.url}}</td> </tr> </table> </div> <script> var m = angular.module('module', []); //factory定義服務(wù)videoServer m.factory('videoServer', ['$http', function ($http) { return { /* 第一種方式 get: function (callback) { $http({url: '1.php'}).then(function (response) { callback(response); }); }*/ //第二種方式 all: function () { return $http({url: '1.php'}); } }; }]); //在控制器中使用videoServer服務(wù),與自帶的服務(wù)使用方式一樣 m.controller('ctrl', ['$scope', 'videoServer', function ($scope, videoServer) { /*第一種方式 videoServer.get(function (response) { $scope.data = response.data; }); */ //第二種方式 videoServer.all().then(function (response) { $scope.data = response.data; }); }]); </script>
1.php
<?php $data = [ [ 'name' => '百度', 'url' => 'www.baidu.com' ], [ 'name' => '谷歌', 'url' => 'google.com' ], ]; echo json_encode($data,JSON_UNESCAPED_UNICODE);
二、使用service來進行自定義服務(wù)
2.html
<div ng-app="module" ng-controller="ctrl"> <table border="1" width="600"> <tr> <td>網(wǎng)站名稱</td> <td>網(wǎng)址</td> </tr> <tr ng-repeat="v in data"> <td>{{v.name}}</td> <td>{{v.url}}</td> </tr> </table> </div> <script> var m = angular.module('module', []); //service自定義服務(wù)videoServer m.service('videoServer', ['$http', function($http){ this.get=function(){ return $http({method:'get',url:'2.php'}).then(function(response){ return response.data; }) } }]) //在控制器中使用videoServer服務(wù),與自帶的服務(wù)使用方式一樣 m.controller('ctrl', ['$scope', 'videoServer', function ($scope, videoServer) { videoServer.get().then(function (data) { $scope.data = data; }); }]); </script>
2.php
<?php $data = [ [ 'name' => '百度', 'url' => 'www.baidu.com' ], [ 'name' => '谷歌', 'url' => 'google.com' ], ]; echo json_encode($data,JSON_UNESCAPED_UNICODE);
以上這篇對angularJs中2種自定義服務(wù)的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解 (上)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來看看吧。2017-07-07分享Angular http interceptors 攔截器使用(推薦)
AngularJS 是一個 JavaScript 框架。它可通過 <script> 標簽添加到 HTML 頁面。這篇文章主要介紹了分享Angular http interceptors 攔截器使用(推薦),需要的朋友可以參考下2019-11-11AngularJS深入探討scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期
這篇文章主要介紹了AngularJS的scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期,較為詳細的分析了scope的作用域、層次結(jié)構(gòu)、繼承及生命周期相關(guān)概念與使用技巧,需要的朋友可以參考下2016-11-11AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用示例
這篇文章主要介紹了AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用的方法,較為詳細的分析了AngularJS的功能、下載及應(yīng)用創(chuàng)建方法,需要的朋友可以參考下2016-11-11詳解AngularJS通過ocLazyLoad實現(xiàn)動態(tài)(懶)加載模塊和依賴
本篇文章主要介紹了詳解AngularJS通過ocLazyLoad實現(xiàn)動態(tài)(懶)加載模塊和依賴 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03AngularJS實現(xiàn)自定義指令及指令配置項的方法
這篇文章主要介紹了AngularJS實現(xiàn)自定義指令及指令配置項的方法,結(jié)合實例形式簡單總結(jié)分析了AngularJS自定義指令及指令配置項的實現(xiàn)技巧,需要的朋友可以參考下2017-11-11