angualrjs html经过$compile后,ng-model失效?

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
</head>

<body  data-ng-app="MetronicApp" ng-controller="AppController" >
<div id="ttt">
</div>

<script src="https://code.angularjs.org/1.5.7/angular.min.js" type="text/javascript"></script>
<script src="http://www.w3school.com.cn/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    var MetronicApp = angular.module("MetronicApp", [
    ]);
    MetronicApp.controller('AppController', ['$scope', '$rootScope', '$timeout', '$location', '$compile' , '$http', function ($scope, $rootScope, $timeout, $location,$compile,$http) {
        var As = $("#ttt");
        var viewHtml =`
            <input class="form-control" ng-model="cxremark"/>
            <input class="form-control" ng-model="data.cxremark"/>
            <button ng-click="testC()">ok</button>
        `;
        As.html(viewHtml);
        $compile(As.contents())($scope);

        $scope.data = {};
        $scope.cxremark = "";
        $scope.testC = function(){
            console.log($scope.cxremark);
            console.log($scope.data.cxremark);
        }

    }]);
</script>


</body>
</html>

问题是我改变两个输入框的值:
图片描述
输出控制台
图片描述
只有第一个有变化,第二个没有变化啊?为什嚒会出现这种问题呢?

阅读 2.5k
1 个回答

$("#ttt").append($compile(angular.element(viewHtml))($scope));

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏