简体   繁体   English

仅更改一个跨度即可在ng-repeat中输入

[英]Change only one span to input inside ng-repeat

I have a simple app, that shows tasks. 我有一个简单的应用程序,可以显示任务。 I would like to double click on a task to edit it, so I want it change from <span> to <input type="text" /> . 我想双击一个任务进行编辑,因此我希望它从<span>变为<input type="text" /> Here's my code: 这是我的代码:

<li ng-repeat="task in tasks">
    <input type="checkbox" ng-checked="task.done" ng-click="taskDone(task.id, !task.done)"/>
    <span ng-dblclick="editTask()" ng-if="!editable">{{::task.task}}</span>
    <input type="text" ng-if="editable" value="{{task.task}}" />
</li>

But when I double click on a task, every <span> changes to <input type="text" /> : 但是当我双击一个任务时,每个<span>更改为<input type="text" />

在此处输入图片说明

Here's that directive that generates the HTML code: 这是生成HTML代码的指令:

export const tasksList = function() {
    return {
        restrict: 'E',
        scope: {
            tasks: "="
        },
        templateUrl: '../dist/views/tasksList.html',
        link: function(scope, element, attrs) {
            scope.$watchCollection('tasks', function(tasks) {
                scope.updateTasks();
            });
        },
        controller: function($scope) {
            const self = this;
            $scope.editable = false;
            $scope.editTask = function() {
                $scope.editable = true;
            };
            $scope.updateTasks = function() {
                self.tasks = $scope.tasks;
            };
            $scope.taskDone = function(id, taskDone) {
                $scope.tasks[id].done = taskDone;
            };
        },
        controllerAs: "$ctrl"
    };
};

How can I change only one <span> to <input type="text" /> ? 如何仅将一个<span>更改为<input type="text" />

Try putting the editable flag on the task, and not the scope. 尝试将editable标志而不是作用域放在任务上。

<li ng-repeat="task in tasks">
    <input type="checkbox" ng-checked="task.done" ng-click="taskDone(task.id, !task.done)"/>
    <span ng-dblclick="editTask(task)" ng-if="!task.editable">{{::task.task}}</span>
    <input type="text" ng-if="task.editable" value="{{task.task}}" />
</li>

...
$scope.editTask = function(task) {
     task.editable = true;
 };
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM