AngularJS中的Provider

來源:互聯網
上載者:User

標籤:os   io   strong   資料   ar   cti   amp   new   

我們要從Dependency Injection(依賴注入)的對象中擷取的資料或者功能,都是Injector給的。

Injector會建立兩種對象:服務 或 專用對象

Injector要知道如何建立這些對象,就要使用者自行去“註冊”。

有五種註冊方法:

Provider, Value, Factory, Service 和 Constant

構建Service

最強大的是Providor,其他方法只不過是Provider在某些條件下的簡化版。

構建specialized objects

specialized objects包括:

controllers, directives, filters 和 animations

一般用各自的API去建立,比如:

module.controller(‘AppleController‘, function () {    // some code});

另外Provider也能夠構建Filter(使得這個filter變得可配置):

// 用Provider構造filterangular.module(‘Tommy‘, []).provider(‘matchMediaFilter‘, function () {  var service = {    // 這裡是可配置的資料    queries: {      xs: ‘screen and (max-width: 480px)‘,      sm: ‘screen and (min-width: 481px) and (max-width: 768px)‘,      md: ‘screen and (min-width: 769px) and (max-width: 992px)‘,      lg: ‘screen and (min-width: 993px) and (max-width: 1200px)‘,      xl: ‘screen and (min-width: 1201px)‘    },    // 這個$get function是AngularJS規定必須的    $get: function (      $window    ) {      return function (unchanged, query, newVal) {        var mq = service.queries[query];        return ($window.matchMedia && $window.matchMedia(mq).matches)          ? newVal          : unchanged;      };    }  };  return service;});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.