【发布时间】:2014-06-09 19:25:48
【问题描述】:
好吧,我想创建一个“summernote”指令(所见即所得编辑器)。
这是模板:
<summernote active="false">
<button class="edit" ng-click="edit()">Edit</button>
<button class="save" ng-click="saveData()">Save</button>
<button class="cancel" ng-click="cancel()">Cancel</button>
<div class="summernote"></div> // here will be loaded the summernote script
</summernote>
指令代码:
...
.directive('summernote', function($compile) {
return {
restrict: 'E',
replace: true, // Not sure about what this code does,
scope: {
active: '='
},
link: function($scope, elem, attrs) {
var $summernote = elem.find('.summernote'),
$edit = elem.find('.edit'),
$save = elem.find('.save'),
$cancel = elem.find('.cancel');
$scope.active = false;
$scope.$watch('active', function(active) {
// switch summernote's & buttons' state
// code ...
});
// here I have the buttons' click event defined
// QUESTION 1: Is there a better way?
// I'm doing this because the code below is not working.
$edit.on('click', function() {...});
$cancel.on('click', function() {...});
$save.on('click', function() {...});
// THIS IS NOT WORKING...
$scope.edit = function() {
alert('edit');
};
$scope.cancel = function() {
alert('cancel');
};
}
}
});
当我点击保存按钮时,我想发送一些数据,所以我在mainController上声明了saveData,但我必须发送div.summernote数据,我不知道该怎么做
<button class="save" ng-click="saveData(getSummernoteDataFromDirectiveScope?)">Save</button>
主控制器:
.controller('MainController', function($scope, myDataFactory) {
$scope.saveSummernoteData(data) {
myDataFactory.updateData('field_name', data);
}
}
主要问题是,如何使用不同的?范围。问题是我想分离指令逻辑(编辑、取消、div.summernote 行为)和“保存”按钮,其逻辑在 MainController(或主 $scope,这是我的烂摊子)中声明。
链接函数的$scope和MainController $scope一样吗??
我想我对这一切有点混乱,所以任何帮助(文档)都将不胜感激。
【问题讨论】: