在 Angularjs 中,如何访问 formController



在表单中重置数据时,希望设置form.setPristine(),但formController尚未在$scope中注册。

这似乎是一个愚蠢的问题,但我如何找到formController

在下面的代码中,获取"类型错误:无法调用未定义的方法'setPristine'

索引.html

        ...
    

字.js

var langMod = angular.module('langMod', []);langMod.controller( 'wordCntl', function($scope,$http,$location) {  数据  $scope.dflt = { wrd_id: '', usr_id: '', ln: '', word: '' };  $scope.orig = {};  $scope.data = {};  拉取记录默认值  $scope.reset = 函数() {    $scope.orig = angular.copy($scope.dflt);    $scope.data = angular.copy($scope.orig);    $scope.wordForm.setPristine();  }  $scope.reset();};                         

我知道到达formController的唯一方法是当它被设置在$scope中时。 但它还没有,我不知道如何找到它。

指令控制器在其他指令之间共享。若要访问它,请在窗体上创建自定义指令,它将成为链接函数的第 4 个属性。

在自定义指令中,您可以执行以下操作:

//inside custom directive
link: function(scope, element, attrs, controller){
  controller.$setPristine();
} 

我意识到这是对你问题的非常字面的回答。真正的解决方案可能是不要调用$scope.reset...你为什么需要?你的形式一开始不是很原始吗?如果不是,是什么让它不原始?

最新更新