有没有办法在不使用指令的情况下验证角度的字段?例如:我想在输入字段上进行以下验证.
我想在调用JavaScript函数时进行以下验证.
我google了一下,发现有一种方法可以使用ng-valid和$ error,但是我没有设法让它工作.
下面的代码是根据我得到的答案之一:
<div ng-app>
<form name='theForm' novalidate>
<input type='text' name='theText' ng-model='theText' ng-pattern='/^[0-9]+$/'/>
<span ng-show='theForm.theText.$error.pattern'>Field can contain only digits</span>
<span ng-show='theText.length<1'>Field must contain a value</span>
<span ng-show='theText%2!=0&&document.getElementsByName("theText").value!=""&&!theForm.theText.$error.pattern&&!theForm.theText.$pristine'>Value must be an even number</span>
<br/><input type='submit' value='Submit' />
</form>
Run Code Online (Sandbox Code Playgroud)
我想把最后一个[span]里面的东西放进一个JavaScript函数中,以使它更通用,最终只改变JS,而不是在条件变化时改变HTML
有人可以建议吗?一个工作的例子会很棒.
有人可以帮助我做一个国家/国家下拉依赖工作的例子吗?
我故意以这种方式创建JSON,因为我希望依赖项是通用的,所以我可以在任何下拉列表中应用它,同时只使用Meta Data而不是HTML.
这是一个链接, 可以看到JSFidlle中的代码示例
HTML
Country:<select data-ng-model="Countries" data-ng-options="country.id as country.name for country in Countries.items">
<option value="">Please select a country</option>
</select>
State: <select data-ng-model="currentItem" data-ng-options="item.id as item.name for item in currentStates.items">
<option value="">Please select a state</option>
</select>
Run Code Online (Sandbox Code Playgroud)
JavaScript代码:
function Controller($scope) {
var Countries = {
"id": "field10",
"items": [{
"id": "10",
"StateGroupID":"0",
"name": "United State"
},
{
"id": "2",
"StateGroupID":"1",
"name": "Canada"
}]
};
var States =
{ "id": "field20",
"StateGroups": [{
"items": [{ "id": "1",
"name": "Alabama"
}, …Run Code Online (Sandbox Code Playgroud)