如何访问$ {3}}的$ valid,$ dirty值?
<!doctype html>
<html ng-app="plunker">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
<script src="example.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="DatepickerDemoCtrl">
<pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
<pre>Valid is: <em>|{{dt.$valid}}|</em></pre>
<pre>Dirty is is: <em>|{{dt.$valid}}|</em></pre>
<div class="form-horizontal">
<input type="text" datepicker-popup="dd-MMMM-yyyy" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" />
<button class="btn" ng-click="open()"><i class="icon-calendar"></i></button>
</div>
<hr />
</div>
</body>
</html>
答案 0 :(得分:1)
诀窍是用<form>
或ng-form
包装您的输入(与Angular相同),最好与控制器处于同一级别(或者至少将控制器包裹为好)并给出表格和输入的名称:
<div ng-form="theForm" ng-controller="DatepickerDemoCtrl">
<input ... name="theInput" />
<span>{{ theForm.theInput.$valid }}</span><!-- this works now -->
</div>
我更喜欢将控制器放在同一元素中或(ng-)Form下面,因为控制器中的代码也可以访问表单并根据validity,dirty等标志执行其他操作。访问表单$scope.formName
,命名输入为$scope.formName.inputName
。