[英]Using Aurelia, update a bound property in one class from another class
Question: I have two classes and want to update a variable in the View of one class by calling a class from another class. 问题:我有两个类,并且想通过从另一个类调用一个类来更新一个类的View中的变量。 Let me elaborate.
让我详细说明。
import {inject} from "aurelia-framework";
import {OtherClass} from "/otherClass";
@inject(OtherClass)
export class ThisClass {
constructor(otherClass){
this.otherClass = otherClass;
}
callLoader(){
this.message = "Hi There!";
this.otherClass.changeMessage(this.message);
}
}
export class OtherClass {
constructor(){
this.oldMessage = "";
}
activate(){
this.oldMessage = "Yo!";
}
changeMessage(message){
this.oldMessage = message;
}
}
<template>
${oldMessage}
</template>
I know the new message is being brought over because when I do a console.log in changeMessage for the passed in message I can see it but the View isn't updating with the new message. 我知道新消息会被传递,因为当我在changeMessage中为传递的消息执行console.log时,我可以看到它,但是View不会随新消息一起更新。 Any help would be greatly appreciated.
任何帮助将不胜感激。
When you inject otherClass
, aurelia creates a singleton for you, but it is not the one with view created by templating engine. 当您注入
otherClass
, otherClass
将为您创建一个单例,但不是具有通过模板引擎创建的视图的单例。
One of the options for you could be pubsub messaging using aurelia-event-aggregator https://stackoverflow.com/a/32207501/3436921 使用aurelia-event-aggregator https://stackoverflow.com/a/32207501/3436921进行发布订阅消息传递是您的一种选择
Also if you have smth like: 另外,如果您有类似的东西:
<my-component ref="myComp"></my-component>
You can call myComp.au.controller.myCompMethod(), but as for me, it is NOT good pattern, if you need to pass the data it's better to use bindings. 您可以调用myComp.au.controller.myCompMethod(),但对于我来说,这不是一个好的模式,如果您需要传递数据,则最好使用绑定。
EDITED: Here is example that shows two ways of communication between components, pubsub messaging and function-call handling. 编辑:这是显示组件之间通讯的两种方式的示例,pubsub消息传递和函数调用处理。 http://plnkr.co/edit/DuDO87wAsgM9QjtGBb9J
http://plnkr.co/edit/DuDO87wAsgM9QjtGBb9J
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.