Skip to content

Instantly share code, notes, and snippets.

@yaegaki
Created May 9, 2014 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaegaki/6a9326686f492e614732 to your computer and use it in GitHub Desktop.
Save yaegaki/6a9326686f492e614732 to your computer and use it in GitHub Desktop.
AngularJSでngModelをinputに付けたときに日本語入力がリアルタイムで反映されない問題 ref: http://qiita.com/yaegaki/items/c9cf111ef9d0c541a194
<input ng-model="hoge" type="text">
<h1>{{hoge}}</h1>
app.directive('jpNgModel', function($parse){
return {
restrict:'A',
link:function(scope, element, attrs){
var getter = $parse(attrs.jpNgModel);
var setter = getter.assign;
var value = getter(scope);
element.bind('input', function(){
setter(scope, element.val());
scope.$apply();
});
scope.$watch(attrs.jpNgModel, function(n){
if(n !== element.val()){
element.val(n);
}
});
}
}
});
<input jp-ng-model="hoge" type="text">
<h1>{{hoge}}</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment