【问题标题】:Angular 2 change data from classAngular 2从类更改数据
【发布时间】:2016-05-11 13:55:25
【问题描述】:

为什么我不能从类中的函数更改视图中的数据?

wrap.component.ts

@Component({
    selector: 'wrap',
    templateUrl: './templates/wrap.html'
})

export class WrapComponent {
    public test: string;
    constructor () {
        this.test = 'Hello world';
    }

    fun () {
        this.test = 'SPAM';
    }
}

express.component.ts

import {WrapComponent} from './wrap.component';
export class Express {
    constructor () {
        new WrapComponent().fun();
    }
}

wrap.html

<h2>{{test}}</h2>

为什么在视图中测试变量是“Hello world”?

【问题讨论】:

    标签: javascript angularjs data-binding typescript angular


    【解决方案1】:

    实际上,您可以自己实例化来自Express 类的WrapComponent 组件。 Angular2 自己管理组件实例。这意味着您不共享同一个实例。

    为了进一步帮助您,我需要知道Express 类代表哪种实体......另一个组件,一个服务......

    您可以从WrapComponent 组件中的click 事件调用fun 方法:

    <div (click)="fun()">Have fun!</div>
    

    希望对你有帮助 蒂埃里

    【讨论】:

    • 完整应用程序作为电子应用程序工作,完整版Express 是带有ExpressJS API 的类。然后这个api获取请求,我运行方法fun。该方法位于其他文件wrap.components.ts中,该文件具有组件和前端业务逻辑。此方法必须更改wrap.html中的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2017-12-02
    • 2016-12-20
    相关资源
    最近更新 更多