【问题标题】:Custom attribute in aurelia no working?aurelia 中的自定义属性不起作用?
【发布时间】:2015-04-16 16:03:22
【问题描述】:

我正在学习 Aurelia 的工作原理,并且我正在尝试让一个简单的自定义属性正常工作。它所要做的就是根据某些值的变化来改变 div 文本的颜色。

我有一个 div,它有:

    high.bind="changeColor"

在我的属性中我有:

import {inject, customAttribute} from 'aurelia-framework';

@customAttribute('high')
@inject(Element)
export class High {
    constructor(element) {
       this.element = element;
   }

   valueChanged(newValue){
   console.log(newValue);
   if (newValue) {
     this.element.classList.remove('highlight-yellow');
   } else {
     this.element.classList.add('highlight-blue');
  }
}

在我的视图模型中,我有:

import {high} from './highlightattribute'


export class Welcome{
  heading = 'Welcome to the Aurelia Navigation App!';
  firstName = 'John';
  lastName = 'Doe';

 get fullName(){
   return `${this.firstName} ${this.lastName}`;
 }

 get changeColor(){
  if (this.firstName == 'John'){
    return false;  
  }
  return true;
 }  
 welcome(){
   alert(`Welcome, ${this.fullName}!`);
 }
}

当我更改名字时,我没有看到在高自定义属性类中触发了 valueChanged 事件。

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    看起来您正在将高代码导入您的视图模型而不是您的视图。在您的 ViewModel 中删除这一行:

    import {high} from './highlightattribute'
    

    然后将此行添加到您的视图中:

    <require from="./highlightattribute"></require>
    

    接下来,在highlightattribute.js 文件中,您将删除highlight-yellow 并添加highlight-blue,因此您可能希望添加和删除同一个类。我还注意到您发布的highlightattribute.js 文件中缺少括号,但这可能只是在复制代码时遗漏了。

    如果这有助于解决问题,请告诉我。我已将您的代码示例推送到此处:https://github.com/AshleyGrant/skeleton-navigation/tree/so-answer-20150416-01/src

    【讨论】:

    • 非常感谢阿什利。我第一次看到 Aurelia,所以我非常感谢您的帮助。我不再需要 angularjs 了 :)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多