我尝试将 Beautifull WYSIWYG Redactor ( http://imperavi.com/redactor/ ) 集成到自定义 AngularJS 指令中。
Visualy 它可以工作,但我的自定义指令与 ng-model 不兼容(我不明白为什么)
这就是你可以使用我的指令的方式:
<wysiwyg ng-model="edited.comment" id="contactEditCom" content="{{content}}" required></wysiwyg>
这是指令代码:
var myApp = angular.module('myApp', []);
myApp.directive("wysiwyg", function(){
var linkFn = function(scope, el, attr, ngModel) {
scope.redactor = null;
scope.$watch('content', function(val) {
if (val !== "")
{
scope.redactor = $("#" + attr.id).redactor({
focus : false,
callback: function(o) {
o.setCode(val);
$("#" + attr.id).keydown(function(){
scope.$apply(read);
});
}
});
}
});
function read() {
var content = scope.redactor.getCode();
console.log(content);
if (ngModel.viewValue != content)
{
ngModel.$setViewValue(content);
console.log(ngModel);
}
}
};
return {
require: 'ngModel',
link: linkFn,
restrict: 'E',
scope: {
content: '@'
},
transclude: true
};
});