有一个简单的形式
<ng-form name="foo">
<button />
</ng-form>
点击按钮应提醒表单名称foo。
Angular 方式来做到这一点吗?
编辑: 告诉我我 parent 的名字是一个可能的解决方案。
最佳答案
假设按钮具有作为其直接父级的表单,您可以利用 ng-click
上的 $event
属性遍历 DOM 并定位父级表单元素.这使用 angular.element(someElement).parent()
。
$scope.showFormName = function($event) {
$scope.formName = angular.element($event.target).parent()[0].name;
};
<button ng-click="showFormName($event)">Show Form Name</button>
http://plnkr.co/edit/yDPc76rqUHpOcL2aGeIU?p=preview
如果按钮不是表单元素的直接后代,或者不在表单本身内,则可能需要更高级的 DOM 遍历,最有可能通过 JQuery,因为这不是设计目标的一部分 Angular 框架。
关于javascript - 在 Angular 中获取表单的名称(this.form.name),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023774/