如何为标题栏定义View-Model?

时间:2017-06-26 13:14:49

标签: javascript angular typescript webstorm aurelia

我想为标题栏创建一个View-Model。我使用WebStorm + TypeScript + Aurelia。我有一个header-bar.html文件,如下所示:

<template bindable="router">
  <require from="_controls/clock"></require>
  <require from="_controls/language-switcher"></require>
  <header class="header" role="banner">
    <div class="header-wrapper">
      <div class="header-back">
        <a href="##" onClick="history.back(); return false;">
          <img src="dist/images/01_back_32x32.png">
        </a>
      </div>

      <clock class="header-clock"></clock>

      <div class="header-signals">
        <img src="dist/images/01_Meldung_64x64.png" alt="Meldung" onclick="$('#notification-window, #cover').show()">
        <img src="dist/images/01_Wartung_Montage_kreis_64x64.png" alt="Wartung">
        <img src="dist/images/01_Aktoren_Kreis_64x64.png" alt="Aktoren">
        <img src="dist/images/01_Lock_64x64.png" alt="Lock">
        <img src="dist/images/01_Batterie_fixnav_64x64.png" alt="Batterie">

        <language-switcher class="language-switcher"></language-switcher>
      </div>

      <a href="#" class="header-logo">
        <img src="dist/images/fischerlogo300.png" alt="Logo Image">
      </a>

    </div>
  </header>

</template>

当我在同一目录和相同名称(header-bar.ts)中创建TypeScript文件(View-Model)时,它们之间没有关系。我无法绑定它们之间的值。例如,以下代码(header-bar.ts):

export class HeaderBar {
  public telNumber: string;
  public hotLineNumber: string;
  public emailAddress: string;
  public constructor() {
    this.telNumber = "+49999999";
    this.hotLineNumber = "01726666";
    this.emailAddress = "service@xxx.de";
  }
}

您能否告诉我,如何为此标题栏创建一个View-Model?

1 个答案:

答案 0 :(得分:1)

首先,我删除了.html:

<require from="header-bar"></require>

但这对我来说还不够。我按照您的说明更改了header-bar.ts

import {autoinject} from "aurelia-dependency-injection";
import {Router} from "aurelia-router";
import {bindable} from "aurelia-templating";

@autoinject
export class HeaderBar {
  public key: string;

  @bindable
  public router: Router;

  constructor() {
    this.key = "one";
  }
}