Polymer dart:数据将整数值绑定到 String 属性 [英] Polymer dart: Data bind integer value to String attribute
问题描述
我正在尝试将一个整数绑定到一个 String 属性.准确地说,我试图将一个已发布的整数变量绑定到文本输入元素的 value 属性.
I am trying to bind an integer to a String attribute. To say exactly, I am trying to bind a published integer variable to the value attribute of the text input element.
@published int 数据 = 0;
@published int data = 0;
<input type="number" value="{{data}}">
显然,String 的引用被存储在一个应该是整数的地方.
Obviously, a reference of String is getting stored in what is supposed to be an integer.
我尝试使用过滤器来解决这个问题,但仍然可以让它工作:
I tried to use filter to solve this issue, but still could get it to work:
int integerize(Object a) {
int ret = 0;
if (a is String) {
try {
ret = int.parse(a);
} on FormatException catch (e) {
}
} else if( a is int) {
ret = a;
}
return ret;
}
<input type="number" value="{{data | integerize}}">
所以我改用不使用绑定.有人可以建议使用绑定的更好、更有效的解决方案吗?
So I switched to not using binding for this. Can somebody suggest a better, efficient solution using binding?
推荐答案
对于 Polymer 1.0.0 这对我来说很好
For Polymer 1.0.0 this worked fine for me
创建一个可重用的行为或只是将 convertToNumeric()
添加到您的 Polymer 元素:
Create a reusable behavior or just add the convertToNumeric()
to your Polymer element:
@HtmlImport('app_element.html')
library app_element;
import 'dart:html' as dom;
import 'package:web_components/web_components.dart' show HtmlImport;
import 'package:polymer/polymer.dart';
@behavior
abstract class InputConverterBehavior implements PolymerBase {
@reflectable
void convertToInt(dom.Event e, _) {
final input = (e.target as dom.NumberInputElement);
double value = input.valueAsNumber;
int intValue =
value == value.isInfinite || value.isNaN ? null : value.toInt();
notifyPath(input.attributes['notify-path'], intValue);
}
}
将行为应用于您的元素:
Apply the behavior to your element:
@PolymerRegister('app-element')
class AppElement extends PolymerElement with InputConverterBehavior {
AppElement.created() : super.created();
@property int intValue;
}
在元素的 HTML 中配置输入元素:
In HTML of your element configure the input element:
- 将
value
绑定到您的属性:value="[[intValue]]"
以便在属性更改时更新输入元素 - 设置事件通知以在值更改时调用转换器
on-input="convertToNumeric" notify-path="intValue"
其中intValue
是要使用数值更新的属性.
- bind
value
to your property:value="[[intValue]]"
so the input element gets updated when the property changes - set up event notification to call the converter when the value changes
on-input="convertToNumeric" notify-path="intValue"
whereintValue
is the name of the property to update with the numeric value.
<!DOCTYPE html>
<dom-module id='app-element'>
<template>
<style>
input:invalid {
border: 3px solid red;
}
</style>
<input type="number" value="[[intValue]]"
on-input="convertToInt" notify-path="intValue">
<!-- a 2nd element just to demonstrate that 2-way-binding -->
<input type="number" value="[[intValue]]"
on-input="convertToInt" notify-path="intValue">
</template>
</dom-module>
<小时>
另一种方法
创建一个属性作为getter/setter:
Create a property as getter/setter:
int _intValue;
@property int get intValue => _intValue;
@reflectable set intValue(value) => convertToInt(value, 'intValue');
创建行为或将函数直接添加到您的元素
Create a behavior or add the function directly to your element
@behavior
abstract class InputConverterBehavior implements PolymerBase {
void convertToInt(value, String propertyPath) {
int result;
if (value == null) {
result = null;
} else if (value is String) {
double doubleValue = double.parse(value, (_) => double.NAN);
result =
doubleValue == doubleValue.isNaN ? null : doubleValue.toInt();
} else if (value is int) {
result = value;
} else if (value is double) {
result =
value == value.isInfinite || value.isNaN ? null : value.toInt();
}
set(propertyPath, result);
}
}
这样您就可以使用与文本输入字段相同的标记
This way you can use the same markup as for text input fields
<input type="number" value="{{intValue::input}}">
或者如果你想延迟属性的更新,直到输入字段被留下
or if you want to delay the update of the property until the input field is left
<input type="number" value="{{intValue::change}}">
这篇关于Polymer dart:数据将整数值绑定到 String 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!