Javascript Ng模型没有';在单击页面上的某个位置之前,不要显示更新的值
我只想显示ngModel的值,ngModel是控制器中定义的变量。但在我单击页面上的其他位置或再次单击“更新”按钮之前,该值没有更改为正确的值。尽管它在控制台中确实更改为正确的值 我的themeLayout.html文件Javascript Ng模型没有';在单击页面上的某个位置之前,不要显示更新的值,javascript,angularjs,amazon-web-services,ecmascript-6,angular-ngmodel,Javascript,Angularjs,Amazon Web Services,Ecmascript 6,Angular Ngmodel,我只想显示ngModel的值,ngModel是控制器中定义的变量。但在我单击页面上的其他位置或再次单击“更新”按钮之前,该值没有更改为正确的值。尽管它在控制台中确实更改为正确的值 我的themeLayout.html文件 <div class="theme-body" ng-controller="ThemeController as theme"> <select ng-model="theme.selectedTheme" ng-change="theme.getThe
<div class="theme-body" ng-controller="ThemeController as theme">
<select ng-model="theme.selectedTheme" ng-change="theme.getThemeDetails(theme.selectedTheme)">
<option ng-repeat="option in theme.themePacks">{{option.title}}</option>
</select>
<div class="form-group">
<label for="appLogo">App Logo</label>
<div>
<img ng-src="{{theme.currentThemeImageUrl}}" alt="Description" />
</div>
<input type="text" class="form-control" id="appLogo" ng-model="theme.currentThemeImageUrl">
</div>
</div>
这是当我单击选择中的fox选项时,它读取数据并将其选通到CurrentSethElection中。
从控制台中可以看到,它还可以打印值
我想的是,可能是“这个”和obj造成了问题
之后,我按照他的建议添加了$scope.apply()函数,但没有解决问题。在
范围内更新您的模型。$apply()
调用:
getThemeDetails(theme) {
this.$log.log(`get theme details function been called`, theme);
const obj = this;
for(let item of this.themePacks) {
if (theme === item.title) {
this.currentThemeId = item.themeId;
}
}
this.s3.getObject({
Bucket: `improd-image-pipeline`,
Key: `remoteUX/qa/${obj.currentThemeId}.json`,
}, (err, data) =>
scope.$apply(() => {
if (err) {
obj.$log.log(err);
} else {
obj.currentS3ThemeOption = JSON.parse(data.Body);
obj.currentS3ThemeOptionToUpload = obj.currentS3ThemeOption;
for (const prop in obj.currentS3ThemeOption.colors) {
obj[prop] = obj.getColors(obj.currentS3ThemeOption.colors[prop]);
}
obj.currentThemeImageUrl = obj.currentS3ThemeOption.layout.titleImageUrl;
obj.$log.log(`We should have upadted theme opion now`, obj.currentS3ThemeOption, obj.currentThemeImageUrl);
}
})
);
this.$log.log(obj.currentS3ThemeOption, this.currentS3ThemeOption);
}
在
范围内更新模型。$apply()
调用:
getThemeDetails(theme) {
this.$log.log(`get theme details function been called`, theme);
const obj = this;
for(let item of this.themePacks) {
if (theme === item.title) {
this.currentThemeId = item.themeId;
}
}
this.s3.getObject({
Bucket: `improd-image-pipeline`,
Key: `remoteUX/qa/${obj.currentThemeId}.json`,
}, (err, data) =>
scope.$apply(() => {
if (err) {
obj.$log.log(err);
} else {
obj.currentS3ThemeOption = JSON.parse(data.Body);
obj.currentS3ThemeOptionToUpload = obj.currentS3ThemeOption;
for (const prop in obj.currentS3ThemeOption.colors) {
obj[prop] = obj.getColors(obj.currentS3ThemeOption.colors[prop]);
}
obj.currentThemeImageUrl = obj.currentS3ThemeOption.layout.titleImageUrl;
obj.$log.log(`We should have upadted theme opion now`, obj.currentS3ThemeOption, obj.currentThemeImageUrl);
}
})
);
this.$log.log(obj.currentS3ThemeOption, this.currentS3ThemeOption);
}
这是消化循环的问题消化循环只有在四种情况下才会触发 事件(即您的点击), http请求, 投入的变化,
带有回调($timeout等)的计时器,这是摘要循环的问题摘要循环只在四种情况下触发 事件(即您的点击), http请求, 投入的变化,
将$scope对象分配到ng模型对象后,具有回调($timeout等)的计时器。那你就把
$scope.$apply();
一旦您将$scope对象分配给ng模型对象,它将与UI元素正确绑定。。那你就把
$scope.$apply();
这样它就可以与UI元素正确绑定。@b谢谢,但是这个作用域是做什么的。$apply()以及为什么我的代码不能工作?
scope。$apply
告诉Angular作用域上的某些内容可能已经更改。通常Angular会为您执行此操作,但有时(例如,当从Angular外部触发fn时)您必须手动执行此操作。请参阅@bchemy,谢谢,但是这个作用域。$apply()有什么作用?为什么我的代码不工作?scope。$apply
告诉Angular作用域上的某些内容可能已更改。通常Angular会为您执行此操作,但有时(例如,当从Angular外部触发fn时)您必须手动执行此操作。请参阅从控制器开始初始化变量或使用$apply
使更改生效。从控制器开始初始化变量或使用$apply
使更改生效。