?
This document uses PHP Chinese website manual Release
創(chuàng)建一個注入函數(shù),用于通過依賴注入的方式獲取服務(參見 依賴注入)。
angular.injector(modules);
參數(shù) | 類型 | 詳述 |
---|---|---|
modules | Array.<string|Function> |
模塊函數(shù)或它們的別名的列表。參見
|
function() |
注入函數(shù)。參見 $injector. |
典型用法
// create an injector
var $injector = angular.injector(['ng']);
// use the injector to kick off your application
// use the type inference to auto inject arguments, or use implicit injection
$injector.invoke(Function($rootScope, $compile, $document){
$compile($document)($rootScope);
$rootScope.$digest();
});
有時你要從Angular外部訪問當前運行的Angular應用程序的注入器。也許,你想在應用啟動后注入和編譯一些標記,你可以使用獨立的injector()
添加到JQuery/jqLite元素。參見 angular.element
.
這是相當少見的,可能的情況是第三方庫作為注入標記。
在下面的例子中,一個包含ng-controller
指令的HTML新區(qū)塊通過JQuery添加到文檔末尾。我們隨后編譯并鏈接它到當前AngularJS域中。
var $div = $('<div ng-controller="MyCtrl">{{content.label}}</div>');
$(document.body).append($div);
angular.element(document).injector().invoke(Function($compile) {
var scope = angular.element($div).scope();
$compile($div)(scope);
});