角1.4.8ディレクティブ要素をプログラムで作成するにはどうすればよいですか?
コントローラー内でプログラムでディレクティブエレメントを作成するにはどうすればよいですか?私は$compileを試しましたが、それは私のためには機能しません。
コントローラとディレクティブ
angular.module('plunker', [])
.controller('MainCtrl', function ($scope, $compile) {
var container = angular.element(document.getElementById('container'));
$scope.user = {item: 'Axe'};
var item = angular.element(document.createElement('anItem'));
item = $compile(item)($scope);
container.append(item);
})
.directive('anItem', function(){
return {
templateUrl: 'template.html'
};
});
template.html
<p>Item: {{user.item}}</p>
index.htmlを
...
<body ng-controller="MainCtrl">
<div id="container"></div>
</body>
...
彼私のプランカです:http://plnkr.co/edit/XY6C6J70PjQTrjiwjHSz?p=preview
あなたのhtmlでng-ifを使用することはできませんか?あなたのコントローラーにフラグを設定してレンダリングするかどうかを決めます。 –
それはちょうどケーシングの問題です: 'anItem'の代わりに' document.createElement( 'an-item') ' – lex82
@ lex82あなたは正しいです、ありがとう!あなたの答えを掲示し、私はそれを評価します。 – trex