为什么在下面的示例中初始渲染的值是{{person.name}}而不是David?你将如何解决这个问题?
原文链接:https://www.f2er.com/angularjs/146237.htmlHTML:
<body ng-controller="MyCtrl"> <div contenteditable="true" ng-model="person.name">{{ person.name }}</div> <pre ng-bind="person.name"></pre> </body>
JS:
app.controller('MyCtrl',function($scope) { $scope.person = {name: 'David'}; }); app.directive('contenteditable',function() { return { require: 'ngModel',link: function(scope,element,attrs,ctrl) { // view -> model element.bind('blur',function() { scope.$apply(function() { ctrl.$setViewValue(element.html()); }); }); // model -> view ctrl.$render = function() { element.html(ctrl.$viewValue); }; // load init value from DOM ctrl.$setViewValue(element.html()); } }; });