angularjs中factory,service,provider 自訂服務的不同

來源:互聯網
上載者:User

一,factory,service,provider自訂服務,services.js

 代碼如下 複製代碼

'use strict';

/* Services */

var phonecatServices = angular.module('phonecatServices', []);

phonecatServices.factory('facetorytest', ['$window',         //factory方式
    function($window){
        var test = {
            firstname:"tank",
            lastname:function(){
                return "zhang";
            }
        };
        $window.alert('aaaa');         //內建服務可以注入
        return test;
    }
]);

phonecatServices.service('servicetest', ['$window',          //service方式
    function($window){
        $window.alert('bbbb');        //內建服務可以注入
        this.firstname = "tank";
        this.lastname = function(){
            return "zhang";
        }
    }
]);

phonecatServices.provider('providertest',[                  //provider方式,內建服務不可以注入
    function(){
        this.test = {
            "firstname":"tank",
            "lastname":"zhang"
        }
        this.$get = function () {
            return this.test;
        };
    }
]);

二,controller調用自訂模組,controllers.js

 代碼如下 複製代碼

'use strict';

/* Controllers */

var phonecatControllers = angular.module('phonecatControllers', []);

//寫過js function的對這種調用方式,很熟悉,服務名稱不能變
function TestCtrl($scope,facetorytest,servicetest,providertest) {
    $scope.facetorytest = facetorytest.firstname+" "+facetorytest.lastname();
    $scope.servicetest = servicetest.firstname+" "+servicetest.lastname();
    $scope.providertest = providertest.firstname+" "+providertest.lastname;
}

//這種調用方式根jquery非常的像,服務名稱也不能變
phonecatControllers.controller('TestCtrl',function($scope,facetorytest,servicetest,providertest) {
    $scope.facetorytest = facetorytest.firstname+" "+facetorytest.lastname();
    $scope.servicetest = servicetest.firstname+" "+servicetest.lastname();
    $scope.providertest = providertest.firstname+" "+providertest.lastname;
});

//以注入的方式來調用,服務名稱可以改變
phonecatControllers.controller('TestCtrl',['$scope',"facetorytest","servicetest","providertest",
    function($scope,facetory111,service111,provider111) {     //自訂,服務名稱
        $scope.facetorytest = facetory111.firstname+" "+facetory111.lastname();
        $scope.servicetest = service111.firstname+" "+service111.lastname();
        $scope.providertest = provider111.firstname+" "+provider111.lastname;
    }
]);www.111cn.net

三,建立app把上面的service和controller接合到一起。app.js

 代碼如下 複製代碼

'use strict';

/* App Module */

var phonecatApp = angular.module('phonecatApp', [
    'ngRoute',
    'phonecatControllers',     //上面定義的controller
    'phonecatServices'         //上面自訂的服務
]);

四,html中顯示

 代碼如下 複製代碼

<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
  <meta charset="utf-8">
  <title>Google Phone Gallery</title>
  <link rel="stylesheet" href="css/app.css">
  <link rel="stylesheet" href="css/bootstrap.css">
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>
  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/services.js"></script>
</head>
<body >

  <div ng-controller="TestCtrl">
      <p>{{facetorytest}}</p>
      <p>{{servicetest}}</p>
      <p>{{providertest}}</p>
  </div>

</body>
</html>

顯示結果:

tank zhang
tank zhang
tank zhang

五,錯誤校正

在網上看到,有人說service是不能注入內建伺服器的,但是實際操作結果是provider是不能注入內建服務的。我用的angularjs的版本是AngularJS v1.2.14

 代碼如下 複製代碼

phonecatServices.provider('providertest',['$window',
    function($window){
        $window.alert('cccc');             //報錯
        this.test = {
            "firstname":"tank",
            "lastname":"zhang"
        }
        this.$get = function () {
            return this.test;
        };
    }
]);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.