コンポーネントで属性バインディングを使用する場合、コントローラに渡されるデータは常に文字列です。私は整数を渡そうとしていますが、文字列から変換して変換スティックを持つのに問題があります。整数をAngularJS 1.5コンポーネントにバインドする
データを$onInit()
に整数として保存しようとしましたが、この機能の外ではデータは元の状態(型と値)に戻ります。私はコンポーネントが一般的なルールとして渡されたデータを変更するべきではないことを理解していますが、これは属性バインディングであり、データは値渡しであるため、適用されませんでした。
function IntegerBindingController() {
this.$onInit = function() {
// Assuming 'number="2"' in the HTML
// This only changes the data inside this function
this.number = parseInt(this.number)
this.typeofNumber = typeof this.number // evaluates to 'number'
this.simpleAdd = this.number + 5 // evaluates to 7
this.dataAdd = this.numberOneWay + 5
console.log(this)
}
this.test = function() {
// this.number is a string inside this function
this.typeofNumber = typeof this.number // evaluates to 'string'
this.simpleAdd = this.number + 5 // evaluates to 25
}
}
私は、コントローラ上の新しいプロパティにデータをコピーすることによってこの問題を解決することができ、誰かがここで何が起こっているかを説明できる場合、私は興味があります。この問題の実際の例については、Plunkerを参照してください。
ようになり、 '=' )?いずれにせよ、 '<'を使っても動作しますが、 '$ onInit()'の 'this.number'を変更することは、その関数のスコープ中にのみ有効です。さらに、オブジェクトを最初に作成する必要がなく、テンプレートを介して整数を渡すことが目標です。 –