Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What is going on with this article?
@yaegaki

AngularJSでngModelをinputに付けたときに日本語入力がリアルタイムで反映されない問題

More than 5 years have passed since last update.
<input ng-model="hoge" type="text">
<h1>{{hoge}}</h1>

上みたいにソースを書き,実際にテキスト入力をするとき,日本語入力を確定しないとmodelに反映されない問題がある.
angularjsの1.2.7以上のバージョンで確認できる.
Angularjs公式の最初のサンプルでも確認することができる.
Win8.1+MicrosoftIME環境,ブラウザはIE,Firefox,Chromeで確認.

とりあえず以下の用なdirectiveを作成してngModelの代わりに使うと回避できる.

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>
20
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
yaegaki
アセンブリからWebまで広くやっています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
20
Help us understand the problem. What is going on with this article?