我的控制器中有一个函数,它将一段HTML(输入)添加到DOM中。
var myApp = angular.module('myApp', []);
myApp.controller('appController', ['$scope',
function($scope) {
$scope.data = {};
$scope.data.value = null;
$scope.addHtml = function() {
$('#section').html('<input type="checkbox" value="false" name="checkbox", ng-model="data.value">');
}
$scope.logData = function() {
alert($scope.data.value);
}
}
]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myApp">
<head>
</head>
<body ng-controller="appController">
<div id="buttons">
<button ng-click="addHtml()">Add HTML</button>
<button ng-click="logData()">Alert Val</button>
</div>
<div id="section">
</div>
</body>
</html>
&#13;
当我运行$scope.addHtml()
时,它会成功将输入添加到#section div
。但是,ng-model
并未绑定到$scope.data
。
在DOM更改后,将此输入值绑定到$scope.data
的简单但有效的方法是什么?