Angularjs 角度指令不更新范围
我正在编写一个指令,该指令访问一个服务以获取页面的翻译字符串。服务将在加载页面时调用服务器并获取字符串,它与控制器一起工作,显示所有内容,但在指令中不工作Angularjs 角度指令不更新范围,angularjs,Angularjs,我正在编写一个指令,该指令访问一个服务以获取页面的翻译字符串。服务将在加载页面时调用服务器并获取字符串,它与控制器一起工作,显示所有内容,但在指令中不工作 var blockUserAction = function () { return { restrict: 'AE', replace: 'true', scope: { theirId: "=theirId" }, contro
var blockUserAction = function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
theirId: "=theirId"
},
controller: function ($scope, translationsService) {
translationsService.init(); //does http request. Already called on mod.run() so redundant here.
$scope.caption = translationsService.translations.Conversation.OptionsMenu.BlockUser.value;
$scope.btnClick = function () {
debugger;
};
},
template: "<a href='#' data-ng-click='btnClick()'>{{caption}}</a>"
};
}
更新
translations.init():
服务方法名为:
this.setTranslations = function (list) {
var ids = getListOfIds(list);
return getTranslationsFromServer(ids).then(function (response) {
setTranslationValues(list, response);
return list;
});
}
更新2
我已经设置了一个监视翻译…值和控制台。记录了我所能看到的一切,指令被创建,范围被分配给空字符串(默认值),所有这些发生后,监视被触发。。。但是没有价值
var myDirective = function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
theirNoid: "=theirNoid"
},
controller: function ($scope, translationsService) {
translationsService.init();
console.log("init called");
$scope.$watch('translationsService.translations.Conversation.OptionsMenu.BlockUser.value', function () {
console.log('hey, myVar has changed!: ' + translationsService.translations.Conversation.OptionsMenu.BlockUser.value);
});
$scope.caption = translationsService.translations.Conversation.OptionsMenu.BlockUser.value;
console.log("caption set");
$scope.btnClick = function () {
console.log('I clicked the button and the value is: ' + translationsService.translations.Conversation.OptionsMenu.BlockUser.value);
};
},
template: "<a href='#' data-ng-click='btnClick()'>{{caption}} eee</a>"
};
}
但如果我点击按钮,日志是:
I clicked the button and the value is: Block user
通常,当您发出http请求时,调用将立即返回,工作将在后台完成。您是否正在初始化TranslationService.translations.Conversation.Options功能表.BlockUser.value 在进行http调用之前设置为null,并且由于调用会立即返回,因此通过将$scope.caption设置为null,您的其余代码将流动。当然,TranslationService.translations.Conversation.OptionsMenu.BlockUser.value将在以后更新,但$scope.caption仍将保留空值
同样,这是基于这样的假设:您正在translationservice.init()中使用http调用,并且没有正确处理返回的承诺 如果您编写scope{theirId:'=theirId'},那么您必须使用它们的id=“5”,而不是theirId=“5”,谢谢!你说得对,我错过了。虽然这不是问题所在,但我有自动取款机:(如果锚定标记中的
其id
绑定到作用域上的某个值,则指令将自动监视该值。有关使用超时更改控制器作用域中的值的小演示,请参阅。我将控制台。在TranslationService上记录您可以记录的所有内容,以查看其中是否有内容。如果您的翻译Service.init()
被设置为返回一个承诺,这比您可能得到的更好。在指令中设置标题变量时,init可能不会返回。但是,这会在控制台中给您一个错误。@Ruben.Canton$watch
在所有$digest
调用上激发,其中一个调用发生在initializati上开启。这并不意味着您观看的内容的值必须更改。translations对象是一个以id(key)的键值对节点结尾的结构一个值,它被初始化为一个空字符串:“我正在做一个HTTP调用,让我把它添加到问题中,让它更容易查看。如怀疑,在你的服务方法调用这个.StEngEngices中,你只是在做HTTP调用。你应该考虑HTTP调用返回的承诺,用它来更新依赖。变量。我建议您阅读$http上的官方文档,希望这对您有所帮助
this.setTranslations = function (list) {
var ids = getListOfIds(list);
return getTranslationsFromServer(ids).then(function (response) {
setTranslationValues(list, response);
return list;
});
}
var myDirective = function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
theirNoid: "=theirNoid"
},
controller: function ($scope, translationsService) {
translationsService.init();
console.log("init called");
$scope.$watch('translationsService.translations.Conversation.OptionsMenu.BlockUser.value', function () {
console.log('hey, myVar has changed!: ' + translationsService.translations.Conversation.OptionsMenu.BlockUser.value);
});
$scope.caption = translationsService.translations.Conversation.OptionsMenu.BlockUser.value;
console.log("caption set");
$scope.btnClick = function () {
console.log('I clicked the button and the value is: ' + translationsService.translations.Conversation.OptionsMenu.BlockUser.value);
};
},
template: "<a href='#' data-ng-click='btnClick()'>{{caption}} eee</a>"
};
}
init called
caption set
hey, myVar has changed!:
I clicked the button and the value is: Block user