2016-12-25 73 views
3

所以我是Angularjs的新手,但我正在嘗試爲用戶可以發表評論的評論區域編寫代碼。爲此,我使用了帶有conteneditable =「true」屬性的div元素。這裏是我的代碼的html:Angularjs ng-model更新輸入元素的變量不適用於div元素

<html> 
    <script src="./includes/angular.min.js"></script> 
    <script src="./includes/angular.route.js"></script> 
    <script src="./includes/angular.sanitize.js"></script> 
    <script src="./includes/test.js"></script> 
    <body ng-app="testapp" ng-controller="cnt1"> 

     <div contenteditable="true" ng-model='comment'>{{comment}} </div> 
     <input ng-model='comment' type='text'/><br> 

     <div>{{comment}}</div>  
    </body>  
</html> 

而這裏的.js代碼

/// <reference path="./angular.min.js" 
/// <reference path="./angular.route.js" 
/// <reference path="./angular.sanitize.js" 
/// <reference path="./jq/jquery-2.0.3.min.js" 
/// <reference path="./moment.min.js" 
/// <reference path="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js" 

var app = angular.module("testapp", ['ngRoute', 'ngSanitize']); 
app.controller("cnt1", function ($scope, $http, $rootScope, $timeout) { 
    $scope.comment = "type here"; 

}); 

我不明白爲什麼NG-模型更新的「註釋」變量輸入元素,但它不更新div元素的變量!任何幫助表示讚賞! 如果$ apply()可以解決這個問題,我應該如何使用它,以便更新div元素中的「comment」變量?

回答