Ho la seguente direttiva ckEditor. In fondo sono due varianti che ho visto dagli esempi su come impostare i dati nell'editor:
app.directive('ckEditor', [function() {
return {
require: '?ngModel',
link: function ($scope, elm, attr, ngModel) {
var ck = null;
var config = attr.editorSize;
if (config == 'wide') {
ck = CKEDITOR.replace(elm[0], { customConfig: 'config-wide.js' });
} else {
ck = CKEDITOR.replace(elm[0], { customConfig: 'config-narrow.js' });
}
function updateModel() {
$scope.$apply(function() {
ngModel.$setViewValue(ck.getData());
});
}
$scope.$on('modalObjectSet', function (e, modalData) {
// force a call to render
ngModel.$render();
});
ck.on('change', updateModel);
ck.on('mode', updateModel);
ck.on('key', updateModel);
ck.on('dataReady', updateModel);
ck.on('instanceReady', function() {
ngModel.$render();
});
ck.on('insertElement', function() {
setTimeout(function() {
$scope.$apply(function() {
ngModel.$setViewValue(ck.getData());
});
}, 1000);
});
ngModel.$render = function (value) {
ck.setData(ngModel.$modelValue);
};
ngModel.$render = function (value) {
ck.setData(ngModel.$viewValue);
};
}
};
}])
qualcuno può dirmi qual è la differenza tra:
ck.setData(ngModel.$modelValue);
ck.setData(ngModel.$viewValue);
E che dovrei uso. Ho guardato la documentazione angolare e si dice:
$viewValue
Actual string value in the view.
$modelValue
The value in the model, that the control is bound to.
Non ho idea di quello che l'autore intendeva quando ha scritto questo nel documento :-(
Quindi in pratica, '$ viewValue' è sempre una stringa? – cdmckay
Come dicono i documenti: '$ viewValue: valore di stringa effettivo nella vista. Quindi sì. – tennisgent
Un'altra nota. Quando un valore '' è vuoto, la proprietà '$ modelValue' è' undefined', mentre '$ viewValue' è' '' 'stringa vuota. Questo può fare la differenza se stai sniffando la "lunghezza" di '$ modelValue' che non funzionerà, ma' $ viewValue' lo farà. – BradGreens