Javascript Can';AngularJS控制器内部的t访问表单
无法从我的控制器访问表单变量,当我尝试通过$scope.locationForm访问它时,我得到了“未定义”,但当我调用console.log($scope)时,我可以在控制台中看到有loactionForm 我的HTML代码Javascript Can';AngularJS控制器内部的t访问表单,javascript,angularjs,Javascript,Angularjs,无法从我的控制器访问表单变量,当我尝试通过$scope.locationForm访问它时,我得到了“未定义”,但当我调用console.log($scope)时,我可以在控制台中看到有loactionForm 我的HTML代码 <div ng-controller="LocationsController as ctrl"> <form class="form-inline" name="locationForm"> <div class="form-
<div ng-controller="LocationsController as ctrl">
<form class="form-inline" name="locationForm">
<div class="form-group">
<!-- <div class="input-group"> -->
<label for="location-name">Название населенного пункта</label>
<input required
name="name"
ng-model="ctrl.location.name" type="text" class="form-control" id="location-name" placeholder="Название населенного пункта">
<label for="location-name">Район</label>
<select required
name="region_id"
ng-model="ctrl.location.region_id"
ng-options="region.id as region.name for region in ctrl.regions" class="form-control" placeholder="Название района"></select>
<input ng-click="ctrl.save()"
ng-disabled="locationForm.$invalid" type="submit" class="btn btn-default" value="Cохранить">
<a class="btn btn-default" ng-click="ctrl.reset()" ng-show="locationForm.$dirty">Сброс</a>
<!-- </div> -->
</div>
</form>
}) 问题是当
位置控制器
初始化时,表单
元素尚未编译。因此,一种可能的破解方法是使用超时,如
function LocationsController($scope, Location, Region, $q, $timeout) {
//then later
$timeout(function(){lc.reset();})
}
我认为问题在于
LocationController
在编译form
元素之前就被初始化了-你知道如何修复它吗?你想实现什么我需要locationForm
的setPristine
方法在重置函数中使用它在超时中调用重置-$timeout(function(){lc.reset();})
function LocationsController($scope, Location, Region, $q, $timeout) {
//then later
$timeout(function(){lc.reset();})
}