質問

角度JSのcontenteditableディレクティブを使用しています。そして、a.butを使用してng-modelタグの値にアクセスしようとします。

これは私がContenteditable Directive

を使用する私のHTMLです。
<span ng-switch on="editgroupChatFieldFlag" style="width: 87%;margin-right: 0;float:left;"  ng-hide="displatSessionTitleFlag==false">   
    <h5 style="float:left;margin: 7px 14px 0;margin-right: 0;">Chat Session : </h5>
    <a ng-switch-when="true" contenteditable="true" strip-br="true" ng-model="chatMessageName" style="margin-right: 0;width: 59%;margin-left: 4px;" ng-keydown="changeNameFunc($event)">{{secondPresonName}}</a>
    <a ng-switch-when="false" style="margin-right: 0;width: 59%;margin-left: 4px;">{{secondPresonName}}</a>         
</span>
.

とこの抑制性指令

app.directive('contenteditable', function() {
    return {
      restrict: 'A', // only activate on element attribute
      require: '?ngModel', // get a hold of NgModelController
      link: function(scope, element, attrs, ngModel) {
        if(!ngModel) return; // do nothing if no ng-model

        // Specify how UI should be updated
        ngModel.$render = function() {
          element.html(ngModel.$viewValue || '');
        };

        // Listen for change events to enable binding
        element.on('blur keyup change', function() {
          scope.$apply(read);
        });
        read(); // initialize
        // Write data to the model
        function read() {
          var html = element.html();
          // When we clear the content editable the browser leaves a <br> behind
          // If strip-br attribute is provided then we strip this out
          if( attrs.stripBr && html == '<br>' ) {
            html = '';
          }
          ngModel.$setViewValue(html);
        }
      }
    };
  });
.

NG-Modelを使用してaタグ値を取得する方法を教えてください。

役に立ちましたか?

解決

スコープの問題を扱っている(ng-switchは子スコープを作成します)。.または$parent.

を使用する必要があります。

$parent Update NG-Modelを使用した$parent.chatMessageName

<a ng-switch-default contenteditable="true" 
   strip-br="true" 
   ng-model="$parent.chatMessageName" >Enter</a>
.

dot/objectの例: JS

--controller
$scope.myType = {
    chatMessageName: ''
};
.

HTML:

<!-- html -->
<a ng-switch-default 
   contenteditable="true" 
   strip-br="true" 
   ng-model="myType.chatMessageName" >Enter</a>
<a>{{"Text "+ myType.chatMessageName}}</a> 
.

自分自身を教育: https://github.com/angular/angular.js/ Wiki /理解 - スコープ


side :質問を投稿しているとき:質問にフィドルを含め(コメントではなく)、不要なコードを取り除き、そしてそれが読みやすくフォーマットします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top