元の投稿は私の問題を単純化したものです。基本的に、特定の要因によって値が異なるため、スコープ内の文字列{{ foo }}
をテンプレート (たとえば、投稿) の変数に割り当てる必要がありました。{{ b }}
@AjayBenival が述べたように、単純なケースは関数で解決されます。これは基本的には
function test($scope) {
var foo = "surprise";
$scope.b = function () {
return foo;
};
}
代わりにfoo
、オブジェクトのプロパティを取得scope
し、関数を補間します。
私が代わりに行ったのは、文字列の変化を監視することです。テンプレート文字列で補間されていないディレクティブを参照してください
return {
template: '<div class="caption">{{ caption }}</div>',
link: function (scope, element, attrs) {
scope.caption = fooService.computeCaption(); // here caption is set to a string '{{ bar }}'
scope.$watch('caption', function(newValue) {
scope.caption = newValue;
$compile(element.contents())(scope); // resolve (interpolate?) the string `{{ bar }}`
});
}