1
我想將jquery minicolors合併到角度指令中。該指令綁定到ngModel的文本框。當我輸入顏色代碼時,手錶檢測到更改的值,但是當我使用colorpicker選擇它時,值不會反映在範圍變量中。可以有人告訴我我做錯了什麼?在angular.js中使用jquery minicolors
<input minicolors="colorPickerSettings" type="text" data-ng-model="category.ToBeConfirmedEventColor">
angular.
module('myApp', [])
.directive('minicolors', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, element, attrs, ngModel) {
//gets the settings object
var getSettings = function() {
return angular.extend({}, scope.$eval(attrs.minicolors));
};
//init method
var initMinicolors = function() {
// debugger
if (!ngModel) {
return;
}
var settings = getSettings();
//initialize the starting value, if there is one
ngModel.$render = function() {
if (!scope.$$phase) {
//not currently in $digest or $apply
scope.$apply(function() {
var color = ngModel.$viewValue;
element.minicolors('value', color);
});
}
};
// If we don't destroy the old one it doesn't update properly when the config changes
element.minicolors('destroy');
// Create the new minicolors widget
element.minicolors(settings);
// Force a render to override whatever is in the input text box
ngModel.$render();
};
// Watch model for changes and then call init method again if any change occurs
scope.$watch(getSettings, initMinicolors, true);
}
};
});
您能否請創建一個工作的重擊者?我試過沒有成功:http://plnkr.co/edit/v4mZvUTkHG7wnHUSmNSu?p=preview –
http://plnkr.co/edit/tz0OuqD8l8380tkdH7pf?p=preview –
這已完成。你應該檢查出http://kaihenzler.github.io/angular-minicolors/ – Alp