DOM更改后,ng-model不会绑定到$ scope变量

时间:2015-04-09 15:13:47

标签: jquery angularjs dom bind angular-ngmodel

我的控制器中有一个函数,它将一段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;
&#13;
&#13;

当我运行$scope.addHtml()时,它会成功将输入添加到#section div。但是,ng-model并未绑定到$scope.data

在DOM更改后,将此输入值绑定到$scope.data的简单但有效的方法是什么?

0 个答案:

没有答案