gpt4 book ai didi

javascript - $scope.form.$setSubscribed 不是函数

转载 作者:行者123 更新时间:2023-11-28 18:20:55 24 4
gpt4 key购买 nike

我有一个带 Angular 表单,但是当我尝试调用 $scope.myForm.$setSubscribed() 时,它崩溃了。我做错了什么?

我收到此错误:

$scope.myForm.$setSubmitted is not a function

根据 docs 应该是可以的.

$setSubmitted();

Sets the form to its submitted state.

我似乎也对行中的 $touched 有一些问题:

ng-if="myForm.myValue.$error.min && (myForm.myValue.$touched || myForm.$submitted)"

那么,有两个问题,

  1. 为什么我无法调用 $setSubscribed()
  2. 为什么我无法从 input 获取 $touched 标志?

angular.module('myApp', []).controller('myController', ['$scope', function($scope){
var vm = this;
vm.submitTheForm = function(){
$scope.myForm.$setSubmitted();
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myController as ctrl">
<form name="myForm" ng-submit="ctrl.submitTheForm()" >
<input type="submit" value="submit" text="foo">
<input type="number" class="" name="myValue" max="10" ng-model="foo" max="10" min="0" number="{}" placeholder="enter a number" />
<div ng-if="myForm.myValue.$error.min && (myForm.myValue.$touched || myForm.$submitted)">Must be a positive number. This should be seen if input is touched or the form is submitted.</div>
<div ng-if="myForm.myValue.$error.max && (myForm.myValue.$touched || myForm.$submitted)">Cannot be larger than 10. This should be seen if input is touched or the form is submitted.</div>
<br/>
<br/>
<tt>myForm.myValue.$valid = {{myForm.myValue.$valid}}</tt><br/>
<tt>myForm.myValue.$touched = {{myForm.myValue.$touched}}</tt><br/>
<tt>myForm.myValue.$error.min = {{myForm.myValue.$error.min}}</tt><br/>
<tt>myForm.myValue.$error = {{myForm.myValue.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$submitted = {{myForm.$submitted}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>
</div>
</div>

最佳答案

您使用的 AngularJS v1.2.23 不支持 $setSubscribed() AngularJS v1.2.3 ,v1.3.0之后才支持

关于javascript - $scope.form.$setSubscribed 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39913052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com