状況: 移植中、または移植中と言うべきかLakshan Perera の Simple jQuery ColorPicker (https://github.com/laktek/really-simple-color-picker) を Angular に移植します。SO で同様の問題を確認した後、コントローラーを介してプラグインのスコープを割り当てる人もいるようですが、これを行う適切な (Angular) 方法は、プラグインをディレクティブにラップすることです。私は近づいています。新しいカスタム属性を介してプラグインをビューに適切にレンダリングできますが、入力値を属性の値 (ng-model) に渡すディレクティブを設定する方法がわかりません。実際の入力は更新されますが、Angular は変更をリッスンしていないため、実際には入力値が更新されたことを認識していません。公式ドキュメントでは、カスタム属性の設定について説明しています (http://docs.angularjs.org/guide/directive)。
望ましい機能 -
<input my-widget="{{color}}" type="text"/> <!-- my-widget instantiates my directive -->
<h1 style="color:{{color}}"></h1> <!-- I would like the input value to dump into the attribute's value, in this case {{color}} -->
これが私がこれまでに持っているものです:
app.directive('myWidget', function(){
var myLink = function(scope, element, attr) {
scope.$watch('element',function(){
var value = element.val();
element.change(function(){
console.log(attr.ngModel); // This is currently logging undefined
// Push value to attr here?
console.log( value + ' was selected');
});
});
var element = $(element).colorPicker();
}
return {
restrict:'A',
link: myLink
}
});
質問: 要素の更新された値を取得するために属性値を設定するにはどうすればよいですか?