Javascript $scope和setPristine()在工厂内不工作
我正在尝试编写一个将当前api调用转换为可在Javascript $scope和setPristine()在工厂内不工作,javascript,angularjs,Javascript,Angularjs,我正在尝试编写一个将当前api调用转换为可在ApiService工厂中重用的工厂的代码。然而,我似乎无法清除表格 那么,我能做些什么来让clearForm函数按原样工作呢 <form name="formName"> <input type="name" ng-model="form.username"></input> <input type="password" ng=model="form.password"></inp
ApiService
工厂中重用的工厂的代码。然而,我似乎无法清除表格
那么,我能做些什么来让clearForm函数按原样工作呢
<form name="formName">
<input type="name" ng-model="form.username"></input>
<input type="password" ng=model="form.password"></input>
<button type="submit" ng-click="submitForm()">Submit</button>
<form>
提交
表单位于$scope
中。如果不使用$scope
,您无法访问它,并且工厂没有$scope
像这样试试
$scope.formName.$setPristine();
您所能做的就是将该表单传递给服务部门并清除它
'clearForm': function(formName) {
formName.$setPristine();
formName.$setUntouched();
}
来自控制器
FormFactory.clearForm($scope.formName);
表单位于
$scope
中。如果不使用$scope
,您无法访问它,并且工厂没有$scope
像这样试试
$scope.formName.$setPristine();
您所能做的就是将该表单传递给服务部门并清除它
'clearForm': function(formName) {
formName.$setPristine();
formName.$setUntouched();
}
来自控制器
FormFactory.clearForm($scope.formName);
谢谢你的回复。但是我不能直接访问ApiService工厂内的
$scope
,我需要在那里调用FormFactory.clearform您不能从工厂访问$scope,但是您可以从controller@vincents将$scope传递给工厂。我像这样传递范围ApiService.Login($scope)
我可以访问scope.form
但不能访问scope.formName
,因此当我将scope传递到FormFactory.clearForm(scope.formName)
时,$setPristine()仍然没有定义。你能提供一把小提琴吗?谢谢你的回复。但是我不能直接访问ApiService工厂内的$scope
,我需要在那里调用FormFactory.clearform您不能从工厂访问$scope,但是您可以从controller@vincents将$scope传递给工厂。我像这样传递范围ApiService.Login($scope)
我可以访问scope.form
但不能访问scope.formName
,因此当我将scope传递到FormFactory.clearForm(scope.formName)
时,$setPristine()仍然没有定义。你能提供一个小提琴吗?