2017-03-21 35 views
0

我想列出項目,雙擊一個項目可以編輯項目。我跟着this answer寫了下面的代碼(JSBin)。只有雙擊可以使項目可編輯

在開始時,所有項目都是隻讀的;雙擊一個項目確實可以編輯它。但是,在編輯它之後,我意識到它不再是隻讀的。我認爲正確的邏輯將是once we have modified one item, it becomes read-only once again, only double-clicking could change it

有誰知道如何修改代碼來實現這一目標?

var app = angular.module('app', []); 
 
    app.controller('Ctrl', ['$scope', function($scope) { 
 
    $scope.items = [{ 
 
    name: "item #1" 
 
    }, { 
 
    name: "item #2" 
 
    }, { 
 
    name: "item #3" 
 
    }]; 
 
    $scope.eEditable = -1; 
 
    }])
input { 
 
    font-size: 20px; 
 
    border: none; 
 
    background-color: transparent; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 
 
    <div ng-app="app" ng-controller="Ctrl"> 
 
    <table> 
 
    <tr ng-repeat="item in items"> 
 
    <td> 
 
    <input type="text" value="{{item.name}} {{$index}}" ng-readonly='$index !== eEditable' ng-dblclick="eEditable = $index" /> 
 
    </td> 
 
    </tr> 
 
    </table> 
 
    {{count}} 
 
    </div>

回答

2

添加ng-blur='eEditable = -1'input

<input type="text" value="{{item.name}} {{$index}}" ng-blur='eEditable = -1' ng-readonly='$index !== eEditable' ng-dblclick="eEditable = $index" /> 

JSBin

+0

它的工作原理,謝謝你......你對我的可選問題的任何想法? – SoftTimur

+0

我沒有看到當前代碼中的任何驗證?也許你可以使用'ng-enter'directive(這不是一個標準的指令,你可以用谷歌的)在'controller'的'function'中完成。 – Arg0n

相關問題