gpt4 book ai didi

javascript - AngularJS无法读取未定义的属性 '$setPristine'

转载 作者:行者123 更新时间:2023-12-03 08:59:07 25 4
gpt4 key购买 nike

尝试重置表单时,出现错误无法读取未定义的属性“$setPristine”。

我的 Controller :

formApp.controller('FormController', ['$scope', 
function ($scope) {

$scope.options = ["Opt1", "Opt2", "Opt3", "Other"];
$scope.formData = {
selectedOption: null,
firstName: "",
lastName: "",
email: "",
phone: "",
fax: "",
comments: ""
};

var origData = angular.copy($scope.formData);


$scope.submit = function () {
// submit code goes here
};

$scope.reset = function () {
$scope.formData = angular.copy(origData);

// $scope.financeForm.$setUntouched();
$scope.financeForm.$setPristine();


};

$scope.reset();

}
]);

我的 HTML(我已经删除了大部分字段以保持最小化):

<form id="financeForm" name="financeForm" ng-submit="financeForm.$valid && submit()" novalidate>
<md-content layout-padding class="autoScroll">
<md-input-container flex md-is-error="financeForm.selectedOption.$invalid && (financeForm.$submitted || financeForm.selectedOption.$dirty)">
<md-select required placeholder="Nature of your Enquiry" ng-model="formData.selectedOption" name="selectedOption" id="selectedOption">
<md-option ng-repeat="opt in options" value="{{opt}}">{{opt}}</md-option>
</md-select>
<div ng-messages="financeForm.selectedOption.$error" ng-if="financeForm.$submitted || financeForm.selectedOption.$touched">
<div ng-message="required">Please your enquiry option.</div>
</div>
</md-input-container>

<md-input-container>
<label>Comments</label>
<textarea ng-model="formData.comments" columns="1" md-maxlength="500"></textarea>
</md-input-container>
</md-content>

<md-button class="md-raised" ng-click="reset();">RESET</md-button>
<md-button class="md-raised md-primary">SUBMIT</md-button>
</form>

我不明白我做错了什么。有人可以帮忙吗?

最佳答案

$setPristine 未定义,因为您需要将其作为服务添加到顶部的函数中

function ($scope,$setPristine){...

这会让函数知道 $setPristine 的含义

另外,Angular JS $setPristine 仅适用于 AngularJS 1.1.*,因此请检查您的版本。

关于javascript - AngularJS无法读取未定义的属性 '$setPristine',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32368630/

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