2017-02-03 1 views
3

私は、角度文書から、にdirective()component()の両方の方法があることを確認しました。参照: https://docs.angularjs.org/api/ng/provider/$compileProvider

は私もドキュメントにそのディレクティブの使用状況を観察するための https://docs.angularjs.org/api/ng/service/$compile

検索:「compileExample」

しかし、私は目的の任意の説明を参照してくださいしないでください。

質問

Q)はなぜ$ compileProviderは、指令方法がありますか?目的や利益は何ですか?

Q)モジュール上にディレクティブを宣言するのとは異なるコンパイラにディレクティブを登録する方法を教えてください。

答えて

1

モジュール上でディレクティブを宣言するのとは異なるコンパイラにディレクティブを登録するにはどうすればよいですか?

module loaderディレクティブを読み込むために、$compileProvider.directive()方法を使用して:

AngularJS Module Loader Source Code

/** 
    * @ngdoc method 
    * @name angular.Module#directive 
    * @module ng 
    * @param {string|Object} name Directive name, or an object map of directives where the 
    * keys are the names and the values are the factories. 
    * @param {Function} directiveFactory Factory function for creating new instance of 
    * directives. 
    * @description 
    * See {@link ng.$compileProvider#directive $compileProvider.directive()}. 
    */ 
    directive: invokeLaterAndSetModuleName('$compileProvider', 'directive'), 

差は、モジュールローダが装飾指示することができることであり、設定前にそれをしません段階。 configフェーズでディレクティブを追加するためのユースケースが見つかりませんでした。

0

AngularJSガイドは、https://docs.angularjs.org/guide/moduleに、状態:

angular.module('myModule', []). 
    value('a', 123). 
    factory('a', function() { return 123; }). 
    directive('directiveName', ...). 
    filter('filterName', ...); 

// is same as 

angular.module('myModule', []). 
    config(function($provide, $compileProvider, $filterProvider) { 
    $provide.value('a', 123); 
    $provide.factory('a', function() { return 123; }); 
    $compileProvider.directive('directiveName', ...); 
    $filterProvider.register('filterName', ...); 
    }); 

をだから我々は少なくとも$compileProviderが工場にあるもの$providerディレクティブにある知っています。

$compileProviderには少なくとも1つの重要な使用例があります:単体テストで指令を模倣することです。これを見るには、このStackOverflowの質問にSylvainの答えをチェックしてください:How do you mock directives to enable unit testing of higher level directive?

関連する問題