javascript - 所有字段编辑的单个编辑超链接选项

标签 javascript angularjs angularjs-directive

我已经在 AngularJS 中创建了一个带有编辑、保存和取消选项的应用程序,该应用程序工作正常,现在我对所有三个字段都有编辑选项,但是我如何才能有一个编辑选项来制作所有的字段可编辑和保存和取消类似

谁能告诉我一些解决办法

DEMO

HTML

<div ng-controller="LocationFormCtrl">
    <h2>Editors</h2>
    <span ng-repeat="location in locations">


    <div class="field">
        <strong>State:</strong>
        <div click-to-edit="location.state"><input ng-model="location.state"/></div>
    </div>


    <div class="field">
        <strong>City:</strong>
        <div click-to-edit="location.city">
            <select ng-model="location.city" ng-options="loc.city as loc.city for loc in selectableCities"></select>
        </div>
    </div>

    <div class="field">
        <strong>Neighbourhood:</strong>
        <div click-to-edit="location.neighbourhood"><input ng-model="location.neighbourhood"/></div>
    </div>
    <hr></hr>


    </span>
</div>

最佳答案

我创建了一个使用你指令的新指令。我也稍微更新了你的指令。 不确定你是否想要这个。

您可以找到 jsFiddle 演示 here .

更新了您的指令,使其在编辑所有模式时不显示单独的保存和取消。

您可以找到更新的 jsFiddle 演示 here .

    app.directive("clickToEditAll",function(){
    var partial = '<div><div class="field">' +
        '<strong>State:</strong>' +
        '<div click-to-edit="myData.state" external-controle="externalControle"><input ng-model="myData.state"/></div>' +
   ' </div>' +


    '<div class="field">' +
        '<strong>City:</strong>' +
        '<div click-to-edit="myData.city" external-controle="externalControle">' +
            '<select ng-model="myData.city" ng-options="loc.city as loc.city for loc in selectableCities"></select>' +
        '</div>' +
    '</div>' +

    '<div class="field">' +
        '<strong>Neighbourhood:</strong>' +
        '<div click-to-edit="myData.neighbourhood" external-controle="externalControle"><input ng-model="myData.neighbourhood"/></div>' +
    '</div>' +
    '<button ng-show="externalControle.editAll !== true" ng-click="editAllClicked()">Edit All</button><button ng-show="externalControle.editAll === true" ng-click="saveAllClicked()">Save</button><button ng-show="externalControle.editAll === true" ng-click="cancelAllClicked()">Cancel</button><hr/></div>'
    var retValue = {
        restrict: "A",
        template:partial,
        scope:{
            myData:"=",
            selectableCities:"=",
            externalControle: "="
        },
        link:function(scope, elements, attributes){
            scope.editAllClicked = function(){
                scope.externalControle.editAll = true;
            }

            scope.saveAllClicked = function(){
                scope.externalControle.saveAll = true;
            }

            scope.cancelAllClicked = function(){
                scope.externalControle.cancelAll = true;
            }
        }
    }
    return retValue;
});

关于javascript - 所有字段编辑的单个编辑超链接选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037266/

相关文章:

javascript - Promisify 基于事件的模式

javascript - 使用 AngularJS 动态显示图像

javascript - AngularJS,Node.js, Node 模块 'phantom' ...注入(inject)错误,angularjs 试图加载我的指令 + 后缀

javascript - Typescript $inject $timeout into a directive ...在 Controller 中工作,而不是在链接中

javascript - 如何通过 google 登录和 firebase web 9 知道用户是否刚刚创建或已经存在?

javascript - 了解 $push 的 upsert

javascript - AngularJS 中多个单选按钮未选择

javascript - AngularJS:将加载器元素添加到 DOM 顶部

javascript - 如何使用 JavaScript 来选择应按向上/向下键滚动的对象?

javascript - Angular Firebase $createUser 函数返回错误