2017-02-22 6 views
0

私はカスタムコンポーネントを開発しており、いくつかの入力属性にいくつかの条件を設定したいと思います。そうする方法はありますか?例えば角でカスタムコンポーネントの属性を要求する方法

、コンポーネントは「MYCOMP」であり、2つの属性が「必須」と私はそれをしたいと思います「オプション」であることを言う:

<my-comp required="value" optional="value"></my-comp> 

この実行に成功

<my-comp required="value"></my-comp> 

このこれもスムーズに実行してください:

はコンソールで何らかのエラーを送信しますが、attr ibute "required"はもちろん "必須"です。コントローラ内の属性の

export class MyCompComponent implements ng.IComponentOptions { 
    public template: string = '<p>TEMPLATE</p>'; 
    public controller: Function = MyCompController; 
    public bindings: any = { 
     required: '@', 
     optional: '@' 
    } 
} 

答えて

0

チェック:

を私はangularJsのtypescriptですバージョン(バージョン1.5.xの)を使用し、バインディング内の属性は、このようなオブジェクトを宣言しています現時点では

app.controller("MyCompController", function($element,$attrs) { 
    if (!$attrs.required) { 
     console.error("BAD my-comp ",$element[0], 
      "my-comp component missing 'required' attribute"); 
    } 
    }) 

DEMO on PLNKR

+0

$ elementと$ attrsのインタフェースは何ですか? たとえば$ logの場合、ng.ILogService – Gianmarco

関連する問題