我創建了一個名爲my-slider-input的自定義元素,該元素計算用戶輸入的值。我需要在角度控制器中使用這些值。我想知道做這件事的最佳做法是什麼?如何將自定義聚合物元素的屬性綁定到angularjs
鑑於這種聚合物元件:
<dom-module id="my-slider-input">
<style>
:host {
display: block;
}
</style>
<template>
<button id="input1">Input 1</button>
<button id="input2">Input 1</button>
<button id="input3">Input 1</button>
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'my-slider-input',
properties: {
value: {
type: Object,
computed: 'computeOutputs()',
reflectToAttribute: true,
notify: true
},
output1: {
type: Number,
reflectToAttribute: true,
notify: true
},
output3: {
type: Number,
reflectToAttribute: true,
notify: true
},
output2: {
type: Number,
reflectToAttribute: true,
notify: true
}
},
listeners: {
'input1.down': 'computeOutputs',
'input2.down': 'computeOutputs',
'input3.down': 'computeOutputs'
},
attached: function() {
//Fired when this component is attached.
this.computeOutputs();
},
computeOutputs: function() {
var aggregateValue = {};
this.output1 = this.complexFunction(1);
this.output2 = this.complexFunction(2);
this.output3 = this.complexFunction(3);
aggregateValue.output1 = this.output1;
aggregateValue.output2 = this.output2;
aggregateValue.output3 = this.output3;
return aggregateValue;
},
complexFunction: function(input) {
//some complex calculations
return 1;
}
});
})();
</script>
我想我的滑蓋輸入的輸出,我知道我可以創建/監聽事件通知。我怎樣才能得到一個angularjs控制器的輸出?
我該如何做這樣的事情?
<div ng-Controller="myController">
<my-slider-input value="{{outputAggregateValue}}"></my-slider-value>
</div>
或
我應該做這樣的事情?
<div ng-Controller="myController">
<my-slider-input output1="{{output1}}" output2="{{output2}}" output1="{{output3}}"></my-slider-value>
</div>
目前在任何一種情況下,$ scope值都不會更新。
如果這不是最好的方法(雙向vs單向),我如何從我的自定義元素中獲取輸出並在控制器中使用它?
這裏有一個jsbin鏈接:http://jsbin.com/kosetiyelu/edit?html,output
提前感謝! -Dan
我相信你給出的答案是聚合物0.5,但有證據的OP使用聚合物1.0。 –
謝謝你的提示。是的,我使用的聚合物1.0感謝指出這一點@ScottMiles – Danprime
@alesc我添加綁定聚合物指令,我自定義的元素,但它仍然沒有約束力。我確信,在reflectToAttribute並通知被設置爲true。我缺少的東西?我跟着指示,你能請,因爲我使用的聚合物1.0看看,看到了嗎?是嗎?http://jsbin.com/jasipaxoxo/edit?html,output – Danprime