【问题标题】:Reset angular form with $setPristine使用 $setPristine 重置角度形式
【发布时间】: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 的模板页面上。

标签: forms angularjs


【解决方案1】:

将表单和 ng-controller 放在同一级别...

<form name="my_form" ng-controller="my_controller">

这对我有用。

(对不起我的英语);-)

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多