javascript - 合并范围后,Angular 中的字段无法正确重复

标签 javascript angularjs angularjs-ng-repeat

我只是使用这种方法组合了我的模型范围:How can I combine multiple scope models in Angular?

但现在它在 fields 对象之外创建了一个单独的 field 对象,这是它以前没有做过的。

HTML

<li order="" class="field-dropped text" ng-repeat="field in theForm.fields" ng-switch="theForm.field.type">
    <div class="field-actions"><i class="icon-edit"></i> <i class="icon-move"></i> <i class="icon-remove"></i>
    </div>
    <h4>{{theForm.field.label}}</h4>
    <em class="field-desc">{{theForm.field.desc}}</em>

    <!-- type: text -->
    <input ng-switch-when="text" type="text" placeholder="" disabled class="span6">

    <!-- type: para -->
    <textarea ng-switch-when="para" type="text" placeholder="" disabled class="span6"></textarea>

    <!-- type: drop -->
    <select ng-switch-when="drop" placeholder="" disabled class="span6"></select>

    <div class="field-options well">
        <label>Field Title</label>
        <input type="text" placeholder="Field Label" class="span8" ng-model="theForm.field.label">
        <label>Field Description</label>
        <textarea class="description span8" type="text" placeholder="Field Description" ng-model="theForm.field.desc"></textarea>
        <label class="checkbox">
            <input type="checkbox" value="" ng-model="theForm.field.req">Required?</label>

        <!-- type: drop -->
        <label ng-switch-when="drop" class="checkbox">
            <input type="checkbox" value="" ng-model="theForm.field.dropOptionsMul">Allow Multiple Choices?</label>
        <label ng-switch-when="drop">Options</label>
        <em ng-switch-when="drop">Enter a new option on each line.</em>
        <textarea ng-switch-when="drop" class="span6" type="text" placeholder="Options" ng-list="&#10;" ng-trim="false" ng-model="theForm.field.dropOptions"></textarea>

    </div>
    <input class="sortOrder" id="" name="" value="" type="hidden">
</li>

JS

angular.module('formApp', [])
    .controller('formController', ['$scope', function($scope) {
        $scope.theForm = {
            formTitle: '',
            formDesc: '',
            fields: []
        };

        $scope.addField = function(ui) {
            var type = ui.draggable.attr("id");
            console.log(type);
            $scope.theForm.fields.push({type: type, label:'Added Form Title', desc:'Added Form Desc', req:false});
            console.log("for-break");
        };
    }])
    .directive('drag', function() {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            $( elem ).draggable({
                helper: "clone",
                activeClass: "ui-state-default",
                hoverClass: "ui-state-hover",
                drop: function( event, ui ) {
                }
            });
        }
    }
    })
    .directive('drop', function() {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            $( elem ).droppable({
                hoverClass: "holder-state-highlight",
                drop: function(event, ui) {
                    //handleDropEvent(event, ui);
                    //sortOrder();
                    scope.$apply(function(){
                        angular.element('#theForm').scope().addField(ui);
                    });
                }
            });
        }
    }
});

JSON 输出

{
  "formTitle": "",
  "formDesc": "",
  "fields": [],
  "field": {
    "label": "The actual field title",
    "desc": "The actual field description"
  }
}

最佳答案

ng-repeat="field in theForm.fields"

field 成为 theForm.fields 中每个对象的“快捷方式”。

因此在 ng-repeat 中,您只需通过 type 即可调用它。

这就像在说

for (i = 0; i < theForm.fields.length; i++) {
    var field = theForm.fields[0];

    // from now on you access it by field
    field.type = 'My Type';
}

关于javascript - 合并范围后,Angular 中的字段无法正确重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490391/

相关文章:

angularjs - 默认子状态与父状态具有相同的 url

angularjs - Angular 重复、跨列突破

angularjs - 从顶部删除项目时如何在AngularJS中保留ng-repeat的滚动位置

javascript - 如何在 ng-repeat 中为变量设置类

angularjs - Angular ng-repeat 中的条件/指令

angularjs - 带有 ChallengeAuthenticator 的 ReSTLet CorsFilter

javascript - 是否可以在 JS 中重命名 "function"?

php - 用mysql填充的多动态组合框

javascript - 在嵌套对象中搜索键并删除它们

javascript - Google 图表 - 图表不显示