注意
この質問は言い換えられ、ここで回答されました:
私は AngularsJS ディレクティブを使って実験しています。単純なディレクティブを作成しましたが、$http 呼び出しからの {{scopevars}} は存在しません。例えば:
コントローラー
function AppCtrl($scope, $routeParams, Card) {
$scope.abc = "this works well";
$scope.user = User.get({userId: 1}, function(user) {
});
}
ユーザーサービス(うまく機能します)
angular.module('userServices', ['ngResource'])
.factory('User', function ($resource) {
return $resource('/user/:userId.json', {}, {
query: {
method: 'GET',
params: {
userId: 'user'
},
isArray: true
}
});
}
これがディレクティブコードです(壊れています):
/**
* Create the <pretty-tag>{{var}}</pretty-tag>
*/
.directive('prettyTag', function($interpolate) {
return {
restrict: 'E',
link: function(scope, element, attrs) {
var text = element.text();
var e = $interpolate(text)(scope);
var htmlText = "<b>" + e + "</b>";
element.html(htmlText);
}
};
結果:
// => In HTML view <pretty-tag>{{abc}}</pretty-tag> => <b>this works well</b>.
// => In HTML view <h1>{{user.name}}</h1> => <h1>Bob</h1>.
// => In HTML view <pretty-tag>{{user.name}}</pretty-tag> => Doesn't work (empty).
<pretty-tag>{{user.name}}</pretty-tag>
$http 呼び出しを介して設定された var が機能しない理由を誰かが理解するのを手伝ってくれますか?