【问题标题】:How to call a function from ng-model along with ng-model value如何从 ng-model 调用函数以及 ng-model 值
【发布时间】:2016-12-09 07:39:08
【问题描述】:

我有一个带有文本字段的 div,我在其中使用 ng-model 捕获值。我想调用一个函数,因为我在文本字段中输入一些日期以进行一些验证。所以我想从 ng- 调用该函数模型。 那么我们如何才能同时拥有 ng-model 值并从中调用函数.. 我们该怎么做。

<input type="datetime" view="date" min-view="date" maxlength="10" 
 format="dd/MM/yyyy" ng-model="$ctrl.dateOfBirth" value = "{{dateOfBirth}}">
 </div>

我还想将函数称为ng-model="$ctrl.check()",因为我想在输入值时检查一个验证。 我试过ng-model="$ctrl.dateOfBirth &amp;&amp; $ctrl.check()"

它不工作......有人可以帮忙

【问题讨论】:

  • 试试ng-model="$ctrl.dateOfBirth; $ctrl.check()",这适用于onclick之类的事件,可能也适用于这里。
  • 它不工作
  • @KevinKloet angular_angular.js?hash=744f2a2…:12545 错误:[$compile:nonassign] 表达式“$root.customerDetails.dateOfBirth;$ctrl.check()”与指令“datePicker”一起使用是不可转让的!给出上述错误

标签: javascript html angularjs meteor angularjs-ng-model


【解决方案1】:

如果你想对你的日期时间进行验证,最好以正确的方式进行,查看官方示例:

<script>
angular.module('dateInputExample', [])
 .controller('DateController', ['$scope', function($scope) {
   $scope.example = {
     value: new Date(2013, 9, 22)
   };
 }]);
</script>
<form name="myForm" ng-controller="DateController as dateCtrl">
<label for="exampleInput">Pick a date in 2013:</label>
<input type="date" id="exampleInput" name="input" ng-model="example.value"
   placeholder="yyyy-MM-dd" min="2013-01-01" max="2013-12-31" required />
<div role="alert">
 <span class="error" ng-show="myForm.input.$error.required">
     Required!</span>
 <span class="error" ng-show="myForm.input.$error.date">
     Not a valid date!</span>
</div>
<tt>value = {{example.value | date: "yyyy-MM-dd"}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>

【讨论】:

  • 这只是示例之一。但在其他情况下,我只有文本字段,我必须有 ng-model 值和调用函数
猜你喜欢
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2016-07-28
  • 1970-01-01
相关资源
最近更新 更多