Kitchensink 是用古老的 Angular.js 1.2 编写的。
所以,如果你在 Chrome 开发者工具中点击 text specific controls DOM 元素,你会看到它的 html 代码是:<div id="text-wrapper" ng-show="getText()">。
让我们在http://fabricjs.com/js/kitchensink/controller.js 中找到这个getText() 函数。事实证明,它使 angular.js 摘要循环监听 fabric.js API 调用(第 108-113 行):
$scope.getText = function() {
return getActiveProp('text');
};
$scope.setText = function(value) {
setActiveProp('text', value);
};
因此,要刷新文本特定控件中的文本,显然,您需要在切换到其他文本时调用 Fabric 的 setActiveProp('text', value)。
text-specific-controls 中文本区域的内容是使用来自http://fabricjs.com/js/kitchensink/app_config.js 的bind-value-to="text" 指令:
kitchensink.directive('bindValueTo', function() {
return {
restrict: 'A',
link: function ($scope, $element, $attrs) {
var prop = capitalize($attrs.bindValueTo),
getter = 'get' + prop,
setter = 'set' + prop;
$element.on('change keyup select', function() {
if ($element[0].type !== 'checkbox') {
$scope[setter] && $scope[setter](this.value);
}
});
$element.on('click', function() {
if ($element[0].type === 'checkbox') {
if ($element[0].checked) {
$scope[setter] && $scope[setter](true);
}
else {
$scope[setter] && $scope[setter](false);
}
}
})
$scope.$watch($scope[getter], function(newVal) {
if ($element[0].type === 'radio') {
var radioGroup = document.getElementsByName($element[0].name);
for (var i = 0, len = radioGroup.length; i < len; i++) {
radioGroup[i].checked = radioGroup[i].value === newVal;
}
}
else if ($element[0].type === 'checkbox') {
$element[0].checked = newVal;
}
else {
$element.val(newVal);
}
});
}
};
});
该指令仅监视绑定属性的 getter 和 setter(在我们的示例中,绑定属性是文本,因此它监视 getText()/setText() 并反映 UI 中的任何更改。