Javascript 无法获取angularJS中动态添加的输入元素的表单输入元素状态 var-app=angular.module('myApp',[]); app.controller('validateCtrl',函数($scope,$compile){ $scope.createField=函数(){ var lname='LastName:Valid:{{myForm.last.$Valid}}; var tdiv=document.createElement(“div”); tdiv.innerHTML=lname; $compile(tdiv)($scope); 文档.getElementById(“容器”).appendChild(tdiv); } }); 验证示例 名字: Valid:{{myForm.first.$Valid}}[此处显示元素状态,以确定元素是否正确有效,但如果使用“创建”按钮动态添加元素,则不会显示元素是否有效。] 创造

Javascript 无法获取angularJS中动态添加的输入元素的表单输入元素状态 var-app=angular.module('myApp',[]); app.controller('validateCtrl',函数($scope,$compile){ $scope.createField=函数(){ var lname='LastName:Valid:{{myForm.last.$Valid}}; var tdiv=document.createElement(“div”); tdiv.innerHTML=lname; $compile(tdiv)($scope); 文档.getElementById(“容器”).appendChild(tdiv); } }); 验证示例 名字: Valid:{{myForm.first.$Valid}}[此处显示元素状态,以确定元素是否正确有效,但如果使用“创建”按钮动态添加元素,则不会显示元素是否有效。] 创造,javascript,angularjs,Javascript,Angularjs,此处,无论元素是否正确有效,都会显示元素状态,但如果我们使用“创建”按钮动态添加元素,则不会显示元素是否有效。这不起作用,因为您将动态输入编译为分离状态,因此它们无法通过FormController注册自身。要使其工作,首先需要将元素插入DOM,然后立即编译它。所以基本上只需移动$compile(tdiv)($scope)向下一行。最后,您应该具备以下条件: <html> <script src="https://ajax.googleapis.com/ajax/lib

此处,无论元素是否正确有效,都会显示元素状态,但如果我们使用“创建”按钮动态添加元素,则不会显示元素是否有效。

这不起作用,因为您将动态输入编译为分离状态,因此它们无法通过
FormController
注册自身。要使其工作,首先需要将元素插入DOM,然后立即编译它。所以基本上只需移动
$compile(tdiv)($scope)向下一行。最后,您应该具备以下条件:

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script>
        var app = angular.module('myApp', []);
        app.controller('validateCtrl', function ($scope, $compile) {
            $scope.createField = function () {
                var lname = '<label>LastName : </label><input type="text" name="last" ng-model = "last" required/> Valid: {{myForm.last.$valid}}<br>';
                var tdiv = document.createElement("div");
                tdiv.innerHTML = lname;
                $compile(tdiv)($scope);
                document.getElementById("container").appendChild(tdiv);
            }
        });
    </script>
    <body>
        <h2>Validation Example</h2>
        <form ng-app="myApp"
              ng-controller="validateCtrl" 
              name="myForm">
            <div id="container">
                Firstname : <input type="text" name="first" ng-model="first" required/>
                Valid: {{myForm.first.$valid}} [ Here element state is displayed whether element is valid or not correctly but if we add element dynamically with create button it is not showing either valid or not. ]

            </div>
            <button type="button" ng-click="createField();" value="Create">Create</button>
        </form>
    </body>
</html>
$scope.createField=函数(){
var lname='LastName:Valid:{{myForm.last.$Valid}}
; var tdiv=document.createElement(“div”); tdiv.innerHTML=lname; 文档.getElementById(“容器”).appendChild(tdiv); $compile(tdiv)($scope); }
这不起作用,因为您编译的动态输入是分离的,因此它们无法通过
FormController
注册自己。要使其工作,首先需要将元素插入DOM,然后立即编译它。所以基本上只需移动
$compile(tdiv)($scope)向下一行。最后,您应该具备以下条件:

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script>
        var app = angular.module('myApp', []);
        app.controller('validateCtrl', function ($scope, $compile) {
            $scope.createField = function () {
                var lname = '<label>LastName : </label><input type="text" name="last" ng-model = "last" required/> Valid: {{myForm.last.$valid}}<br>';
                var tdiv = document.createElement("div");
                tdiv.innerHTML = lname;
                $compile(tdiv)($scope);
                document.getElementById("container").appendChild(tdiv);
            }
        });
    </script>
    <body>
        <h2>Validation Example</h2>
        <form ng-app="myApp"
              ng-controller="validateCtrl" 
              name="myForm">
            <div id="container">
                Firstname : <input type="text" name="first" ng-model="first" required/>
                Valid: {{myForm.first.$valid}} [ Here element state is displayed whether element is valid or not correctly but if we add element dynamically with create button it is not showing either valid or not. ]

            </div>
            <button type="button" ng-click="createField();" value="Create">Create</button>
        </form>
    </body>
</html>
$scope.createField=函数(){
var lname='LastName:Valid:{{myForm.last.$Valid}}
; var tdiv=document.createElement(“div”); tdiv.innerHTML=lname; 文档.getElementById(“容器”).appendChild(tdiv); $compile(tdiv)($scope); }
如果这个答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。如果这个答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。