16

そのため、Angular 1.1.4 以降では、動的なテンプレート URL を使用できます。ここから、

templateUrl - テンプレートと同じですが、テンプレートは指定された URL から読み込まれます。テンプレートの読み込みは非同期であるため、テンプレートが読み込まれるまでコンパイル/リンクは中断されます。

templateUrl は、URL を表す文字列として指定するか、2 つの引数 tElement と tAttrs (以下のコンパイル関数 API で説明) を取り、URL を表す文字列値を返す関数として指定できます。

これを利用して、ディレクティブの属性などに基づいて動的テンプレートを生成するにはどうすればよいですか? tAttrs.templateType は単に文字列 "templateType" であるため、明らかにこれは機能しません。

templateUrl: function (tElement, tAttrs) {
  if (tAttrs.templateType == 'search') {
    return '/b/js/vendor/angular-ui/template/typeahead/typeahead.html'
  } else {
    return '/b/js/vendor/angular-ui/template/typeahead/typeahead2.html'
  }
}

スコープにアクセスできない場合、どうすればこれを管理できますか?

4

4 に答える 4

1

したがって、問題は typeahead ディレクティブをハッキングする方法にありました... typeahead にスコープ変数を設定し、typeaheadPopup ディレクティブで評価しました。代わりに、templateType 属性を文字列として直接渡し、それを評価しました。例えば

var popUpEl = angular.element(
  "<typeahead-popup " +
    "matches='matches' " +
    "active='activeIdx' " +
    "select='select(activeIdx)' " +
    "template-type='" + attrs.templateType + "'" +
    "query='query' " +
    "position='position'>" +
  "</typeahead-popup>");

それ以外の"template-type='templateType'"

于 2013-06-13T14:11:31.860 に答える