Javascript AngularJS ng类没有';t更新
我在ng课上遇到了问题 这是我的控制器的一部分Javascript AngularJS ng类没有';t更新,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我在ng课上遇到了问题 这是我的控制器的一部分 export class UIController { public static $inject = []; public commands: Array<string> = ['showMap', 'showGantry', 'showSign']; public showMap: boolean = false; public showGantry: boolean = false; pu
export class UIController
{
public static $inject = [];
public commands: Array<string> = ['showMap', 'showGantry', 'showSign'];
public showMap: boolean = false;
public showGantry: boolean = false;
public showSign: boolean = false;
public loadingMessage: string;
constructor()
{
this.showMap = true;
}
public toggleContent(commandToExecute: string)
{
for (var command of this.commands) {
if (command != commandToExecute) {
this[command] = false;
}
else if (!this[command]) {
this[command] = true;
}
}
}
}
和一段HTML视图:
<div class="accordion-button" data-ng-click="uiCtrl.toggleContent('showGantry')">Gantry</div>
<div class="accordion-content" data-ng-class="{'show-content':uiCtrl.showGantry}">test</div>
调用toggleContent
方法后,在spoolmap
中运行scope.$apply()
(仅用于测试)时,ng类被更新,但它会导致错误,如apply已经在进行中
为什么当我在指令中调用适当的控制器方法时,ng类
不更新
编辑
使用timeout
解决了问题。这不是问题的解决方案,但是如果您能够提供一个JSFIDLE示例来重现这个问题,那么您可以重构切换内容函数以使用'map'Hey@pavon147。切换内容(commandToExecute){this.commands=this.commands.map(function(map){return command===commandToExecute?true:false;}}@Rorschach120谢谢!@Kaloyan Kosev-当我在link
from指令中运行toggleContent
时,但在timeout
之后,不是从scope.$root.on…
它工作了,所以问题来自于那个监听器…哦,我指的是函数(command)…在电话上不是问题的解决方案,但是如果您能够提供一个JSFIDLE示例来重现问题,那么您可以重构切换内容函数以使用'map'Hey@pavon147。切换内容(commandToExecute){this.commands=this.commands.map(函数(map){return command==commandToExecute?true:false;})}@Rorschach120谢谢!@Kaloyan Kosev-当我从指令运行toggleContent
时,但在timeout
之后,不是从scope.$root.on…
它起作用了,所以问题来自于那个监听器…哦,我指的是手机上的函数(命令)
<div class="accordion-button" data-ng-click="uiCtrl.toggleContent('showGantry')">Gantry</div>
<div class="accordion-content" data-ng-class="{'show-content':uiCtrl.showGantry}">test</div>
scope.uiCtrl.loadingMessage = 'test'; //it works correctly BTW
scope.$root.$on('gantry-details', (event: IAngularEvent, message: any) =>
{
this._timeout(() =>
{
scope.uiCtrl.toggleContent('showGantry');
});
});