In this official example,以下代码是做什么的?
// model -> view ctrl.$render = function() { elm.html(ctrl.$viewValue); };
As far as I can see,$ render从不调用。
当$ render被实际调用时?
UPDATE
当模型更改时,Right $ render由Angular调用。根据
$render docs:
原文链接:https://www.f2er.com/angularjs/144452.html$render()
Called when the view needs to be updated. It is expected that the user
of the ng-model directive will implement this method.
在ngModelWatch中调用$ render是有帮助的(每当ngModel更改时调用它)。在这里我们看到$formatters
被调用,然后$ viewValue被更新,最后$ render被调用:
$scope.$watch(function ngModelWatch() { var value = ngModelGet($scope); // if scope model value and ngModel value are out of sync if (ctrl.$modelValue !== value) { var formatters = ctrl.$formatters,idx = formatters.length; ctrl.$modelValue = value; while(idx--) { value = formatters[idx](value); } if (ctrl.$viewValue !== value) { ctrl.$viewValue = value; ctrl.$render(); } } return value; }); }];
原来没有调用初始值的原因是因为在指令末尾的这一行:
// load init value from DOM ctrl.$setViewValue(elm.html());
手动更新视图值而不触发ngModelWatch(),因此不经过$ formatter或$ render。如果该行代替:
scope.content=elm.html();
您会看到Angular调用的$ render,因为会触发$ watch