简体   繁体   English

从父元素AngularJS中获取多个输入字段的值

[英]Get Values of multiple input fields from within parent element AngularJS

Firstly apologies if this has been asked before, but I have the following HTML which is generated dynamically by an angular directive (IE When the user clicks a button, a new element is added to the page), all these entries are within a container div. 首先道歉,如果以前已经问过了,但是我有以下由Angular指令动态生成的HTML(即IE,当用户单击按钮时,页面上将添加一个新元素),所有这些条目都在容器div中。

<div class="entry"><tr><td><input type="text" name="firstname"/></td>' +
        '<td><input type="text" name="surname"  /></td></tr></div>

Once the user submits, I want to be able to get the data from both the inputs within each entry. 用户提交后,我希望能够从每个条目中的两个输入中获取数据。

Does anybody have any suggestions on how to do this? 有人对此有任何建议吗?

Thank You! 谢谢!

It is suggested that you use ng-model and ng-submit in the directive. 建议您在指令中使用ng-model和ng-submit。

<script>
    angular.module('MyApp', []).directive('myDirective', function () {
        return {
            template: '<form ng-submit="submit()">' +
                    '<div class="entry">' +
                        '<tr>' +
                            '<td><input type="text" ng-model="firstname" /></td>' +
                            '<td><input type="text" ng-model="surname" /></td>' +
                            '<td><input type="submit" value="Submit" /></td>' +
                        '</tr>' +
                    '</div>' +
                '</form>',
            link: function (scope, element) {
                scope.submit = function() {
                    console.log("scope.firstname", scope.firstname);
                    console.log("scope.surname", scope.surname);
                };
            }
        };
    });
</script>
<div ng-app="MyApp">
    <div my-directive></div>
</div>

Then, you can do anything else in the submit function with firstname and surname. 然后,您可以在提交函数中使用名字和姓氏执行其他任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM