如果在 route 指定了搜索词,我想在页面加载时提交搜索表单。问题在于,当 search()
运行时,searchForm
尚未定义。我见过其他人通过将 ng-controller 放在表单元素上来实现此功能,但我在此页面上有多个表单,因此 Controller 必须是表单的父级。
如何确保在调用 search() 时定义表单?
myModule.controller('MyController', ['$scope', '$routeParams',
function($scope, $routeParams){
$scope.model = {searchTerms: ""};
$scope.search = function(){
if($scope.searchForm.$valid){
...
}
};
if($routeParams.terms !=""){
$scope.model.searchTerms = $routeParams.terms;
$scope.search();
}
}]);
查看:
<div ng-controller="MyController">
<form name="searchForm" ng-submit="search()">
...
</form>
<form name="detailForm" ng-submit="save()">
...
</form>
</div>
最佳答案
这似乎有效:
$scope.$on('$routeChangeSuccess', function () {
if($routeParams.terms !=""){
$scope.model.searchTerms = $routeParams.terms;
$scope.search();
}
});
关于javascript - 在 Angular 中加载页面时提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25332820/