如何访问bootstrap-ui datepicker的$ dirty,$ dirty值?

时间:2013-10-22 07:16:55

标签: angularjs angular-ui-bootstrap

如何访问$ {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>

bootstrap-ui datepicker

1 个答案:

答案 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

叉子:http://plnkr.co/edit/nHRuDgoktX9xclex4DDH?p=preview