를 들어 폴리머 1.0.0이 나를 위해 잘 작동
재사용 가능한 동작을 만들거나 당신의 폴리머 요소에 convertToNumeric()
을 추가
를
@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);
}
}
동작 적용 t 오 당신의 요소 : 귀하의 재산에
- 바인드
value
: @PolymerRegister('app-element')
class AppElement extends PolymerElement with InputConverterBehavior {
AppElement.created() : super.created();
@property int intValue;
}
당신의 요소의 HTML에서 입력 요소를 구성 value="[[intValue]]"
때문에 속성이
- 이벤트를 설정 변경할 때 입력 요소가 업데이트됩니다 값이 변경 될 때 변환기를 호출하는 알림
on-input="convertToNumeric" notify-path="intValue"
여기서 intValue
은 숫자 값으로 업데이트 할 속성의 이름입니다.
<!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>
다른 방법
이 게터/세터와 같은 속성 만들기 :
int _intValue;
@property int get intValue => _intValue;
@reflectable set intValue(value) => convertToInt(value, 'intValue');
이 동작을 만들거나 요소
에 직접 함수를 추가를,210
@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);
}
}
텍스트 입력을위한 같은 마크 업을 사용할 수있는이 방법은
<input type="number" value="{{intValue::input}}">
필드 또는 속성의 업데이트를 지연 할 경우 입력 필드는 왼쪽 때까지
<input type="number" value="{{intValue::change}}">