我有一个像这样的HTML代码段:
<div ng-controller="FormCtrl">
<form name="{{formName}}"></form>
</div>
和像这样的控制器
angular.module('myApp')
.controller('FormCtrl', function ($scope) {
$scope.formName = "aFormName";
$scope.aFormName.$dirty = false // this works
$scope[formName].$dirty = false // does not work, I get undefined.
});
问题是我想根据变量获取表单,因为名称本身可能会在以后更改。
答案 0 :(得分:0)
当我试图抓住表单时,我忘了在变量名之前添加$scope
。我必须这样做:$scope[$scope.formName].$dirty = false
angular.module('myApp')
.controller('FormCtrl', function ($scope) {
$scope.formName = "aFormName";
$scope.aFormName.$dirty = false // this works
$scope[$scope.formName].$dirty = false // Now it works
});