12

contenteditable 属性を使用して、電話のリストで電話名を更新しようとしています。ng-change を使用してみましたが、解雇されません。これを行う方法はありますか?

私はのリストを持っていますStore.Phones

<ul class="store">
  <li ng-repeat="Phone in Store.Phones">
     <strong contenteditable> {{Phone.Name}}</strong>
  </li>
<ul>

そのため、電話名を編集するときに、リストで更新する必要があります。

要素を指すモデルでこのようなことを試しました。これは機能していません。

<strong ng-model='Store.Phones[$index].Name'> {{Phone.Name}}</strong>

また

<strong ng-model='PhoneName' ng-change='PhoneNameChanged()'> {{Phone.Name}}</strong>

ただし、この場合、メソッドは起動されません。

4

2 に答える 2

17

編集

を使用する Angular ドキュメントの例に基づいた例を次に示しますng-repeatng-repeat反復ごとに新しいスコープが作成されるため、問題にはなりません。

<!doctype html>
<html ng-app="form-example2">
<head>
    <script src="http://code.angularjs.org/1.0.5/angular.min.js"></script>
    <script>
    angular.module('form-example2', []).directive('contenteditable', function() {
        return {
            require: 'ngModel',
            link: function(scope, elm, attrs, ctrl) {
                // view -> model
                elm.bind('blur', function() {
                    scope.$apply(function() {
                        ctrl.$setViewValue(elm.html());
                    });
                });

                // model -> view
                ctrl.$render = function() {
                    elm.html(ctrl.$viewValue);
                };

                // load init value from DOM
                ctrl.$setViewValue(elm.html());
            }
        };
    });
    </script>
</head>
<body>
    <div ng-repeat="i in [1, 2, 3]">
        <div contentEditable="true" ng-model="content" title="Click to edit">Some</div>
        <pre>model = {{content}}</pre>
    </div>
    <style type="text/css">
    div[contentEditable] {
        cursor: pointer;
        background-color: #D0D0D0;
    }
    </style>
</body>
</html>

オリジナル

ここにそれを行う方法の例があります: http://docs.angularjs.org/guide/forms

「カスタム フォーム コントロールの実装 (ngModel を使用)」ヘッダーの下にあります。

于 2013-02-27T09:40:06.370 に答える
4

取り除くだけctrl.$setViewValue(elm.html());

http://jsfiddle.net/blingz/B5UbE/12/へのリンク

于 2013-05-27T09:41:38.163 に答える