Angularjs 如何获取contentEditable元素';s在angular js中的更新值?

Angularjs 如何获取contentEditable元素';s在angular js中的更新值?,angularjs,forms,html-table,Angularjs,Forms,Html Table,var myApp=angular.module(“problemApp”,[]); 控制器(“RESTCall”、函数($scope、$compile、$element、$timeout){ $scope.username='Adan'; console.log($scope.username) $scope.printValue=函数(数据){ $timeout(函数(){ $scope.$apply(函数(){ 控制台日志(数据); }); }); } }); 表td,th{

var myApp=angular.module(“problemApp”,[]);
控制器(“RESTCall”、函数($scope、$compile、$element、$timeout){
$scope.username='Adan';
console.log($scope.username)
$scope.printValue=函数(数据){
$timeout(函数(){
$scope.$apply(函数(){
控制台日志(数据);
});
});      
}
});
表td,th{
边框:1px纯黑;
}

名字
{{username}}
姓氏
史密斯

请忽略我之前的回答。我们需要在自定义指令中包装
contenteditable
,因为
contenteditable
指令不能直接与angular的
ng模型
一起工作,仅仅是因为它在每次更改时重新呈现dom元素,导致最近的
ng模型
更改丢失

这里有一个新的
包装
围绕
contenteditable

var-app=angular.module('app',[]);
app.directive('contenteditable',function(){
返回{
限制:“A”,
要求:'ngModel',
控制器:功能($scope){
$scope.username='Adan';
$scope.printValue=函数(){
log($scope.username);
}
},
链接:函数(范围、元素、属性、myController){
on('keyup',function(){
范围:$apply(updateViewModel);
});
函数updateViewModel(){
var htmlValue=element.text()
myController.$setViewValue(htmlValue);
scope.printValue();
}
myController.$render=updateHtml
函数updateHtml(){
var viewModelValue=myController.$viewValue
元素。文本(viewModelValue);
}
}
};
});

{{username}}
名字
{{username}}
姓氏
史密斯

编辑了带有给定答案的代码段。它不工作。当然,让我看看它对angular1.X有效,但是Angular2.0出现了问题