ng-list中的contenteditable项目的双向绑定

Pha*_*ani 12 contenteditable angularjs angularjs-ng-repeat

我希望使用contenteditable属性更新电话列表中的电话名称.我尝试过使用ng-change但是没有被解雇.有什么方法可以做到这一点吗?

我有一份清单 Store.Phones

<ul class="store">
  <li ng-repeat="Phone in Store.Phones">
     <strong contenteditable> {{Phone.Name}}</strong>
  </li>
<ul>
Run Code Online (Sandbox Code Playgroud)

所以现在当我编辑电话名称时,我需要在列表中更新它.

我尝试了类似这样的东西,模型指向元素.这不起作用.

<strong ng-model='Store.Phones[$index].Name'> {{Phone.Name}}</strong>
Run Code Online (Sandbox Code Playgroud)

<strong ng-model='PhoneName' ng-change='PhoneNameChanged()'> {{Phone.Name}}</strong>
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,该方法不会被解雇.

And*_*ahl 16

编辑

这是一个基于Angular文档中刚刚使用的示例的示例ng-repeat.由于ng-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>
Run Code Online (Sandbox Code Playgroud)

原版的

这里有一个如何做到这一点的例子:http://docs.angularjs.org/guide/forms

它位于"实现自定义表单控件(使用ngModel)"标题下.