我正在尝试在范围内获得角度形式来验证验证等。
基本情况
假设我有以下 HTML:
<body ng-controller='MyAwesomeController'>
<form name="fooForm">
<textarea ng-model="reason" required=""></textarea>
</form>
<div class='btn btn-primary' ng-click="submit()" ng-class="{'btn-disabled': true}">Awesome Submit Button</div>
</body>
以及以下控制器
angular.module('MyAwesomeController', '$scope', function(scope){
scope.submit = function(){
console.debug(scope)
}
})
这将起作用,并且经过检查,scope
将包含一个fooForm
key.
现在让我们说我将一个 Angular ui Bootstrap 模式引入到混合中,如下所示:
破案
<body ng-controller="AwesomeParentController">
<div class="btn btn-primary" ng-click="open()">Open the Modal</div>
</body>
具有以下两个控制器:
.controller('AwesomeParentController', ['$scope', '$modal', function(scope, modal){
scope.open = function(){
options = {
windowClass: 'modal discontinue-modal',
templateUrl: 'modal.html',
controller: 'AwesomeModalController'
}
modalInstance = modal.open(options)
modalInstance.result.then(function(){
console.debug("result!")
})
}
}])
.controller("AwesomeModalController", ['$scope', '$modalInstance', function(scope, modalInstance){
scope.submit = function(){
console.debug(scope)
}
}])
具有以下 modal.html:
<form name="fooForm">
<textarea ng-model="reason" required=""></textarea>
</form>
<div class='btn btn-primary' ng-click="submit()">Awesome Submit Button</div>
单击第一个按钮时,将打开一个模式,单击第二个按钮时将打印一个scope
,其中不包含fooForm
, 相当fooForm
驻留在scope.$$childTail
Plunkr: http://embed.plnkr.co/jFGU0teIbL3kUXdyTPxR/preview http://embed.plnkr.co/jFGU0teIbL3kUXdyTPxR/preview
可能的修复
<form name="fooForm">
<div ng-controller ="JankyFormController">
<textarea ng-model="reason" required=""></textarea>
<div class='btn btn-primary' ng-click="submit()">Awesome Submit Button</div>
</div>
</form>
.controller('JankyFormController', ['$scope', function(scope){
scope.models['fooForm'] = scope.fooForm
}])
Plunkr: http://embed.plnkr.co/BAZFbS7hFRhHm8DqOpQy/preview http://embed.plnkr.co/BAZFbS7hFRhHm8DqOpQy/preview
为什么表格被屏蔽?无需创建嵌套子控制器即可获得它的干净方法是什么?如果我想绑定怎么办ng-class
表格的有效性?我现在是否必须围绕($$childTail).fooForm.$valid
?