【发布时间】:2013-10-17 15:45:49
【问题描述】:
使用 $scope.form.$setPristine() 返回未定义。
<html>
<form name="my_form">
...
</form></html>
在我的 $routeProvider 上定义控制器
.when('page_name', {
templateUrl: '...',
controller: function($scope) {
console.log(my_form); // returns the form object
my_form.$setPristine(); returns Object #<HTMLFormElement> has no method '$setPristine'
console.log($scope.my_form); // returns undefined
$scope.my_form.$setPristine(); // returns Cannot call method '$setPristine' of undefined
}
}
我还尝试通过 jQuery $scope.my_form = $('form'); 将表单传递给 $scope,正如预期的那样,它只是发送了表单对象并导致了第一个错误。怎样才能让这种方法发挥作用?
我尝试将 Plunker 放在一起进行演示,但我似乎无法让路由在那里正常工作。
关于表单的其他所有内容都可以正常工作。我只想知道如何让 $setPristine 工作。
【问题讨论】:
-
使用 Angular 1.2.0-rc.2
-
你的
ng-view在哪里? -
@Cherniv 在页面上。 html 显然被截断了。或者更确切地说,在父页面上。该表单位于传递给 ng-view 的模板页面上。