【问题标题】:Editing Nested Select Options Once Added to Repeated List添加到重复列表后编辑嵌套选择选项
【发布时间】:2018-10-26 18:07:33
【问题描述】:

我正在尝试创建一个表单,该表单捕获国家/州/城市的嵌套流并推送到 ng-repeat 对象。将其添加到中继器后,我希望可以选择编辑该嵌套选择。

我不明白将嵌套对象添加到中继器后 ngModel 和 ngOptions 是如何发挥作用的。我尝试了几个选项都没有成功。

我添加了一个 plunker 来帮助解释这个问题: http://next.plnkr.co/edit/FWLa3ErI83JLR4Jy

这是有问题的部分无法正常工作:

HTML

<tr ng-repeat="location in locations">
                <td>
                    <select ng-show="editable" name='CountryName' id="CountryName" class="form-control" ng-model="country" ng-options="country as country.CountryName for country in countries"
                 required>
            <option value="" disabled>Select</option>
        </select>
                    <span ng-show="!editable">{{location.CountryName}}</span>
                </td>
                <td>
                    <select ng-show="editable" name='StateName' required id="StateName" class="form-control" ng-disabled="states.length == 0" ng-model="state" ng-options="state as state.StateName for state in states">
            <option value="" disabled>Select</option>
        </select>
                    <span ng-show="!editable">{{location.StateName}}</span>
                </td>
                <td>
                    <select ng-show="editable" name='CityName' required id="CityName" class="form-control" ng-disabled="cities.length == 0" ng-model="city" ng-options="city as city.CityName for city in cities">
            <option value="" disabled>Select</option>
        </select>
                    <span ng-show="!editable">{{location.CityName}}</span>
                </td>
                <td><a class="btn btn-link" ng-click="editable = !editable">Edit</a></td>
            </tr>

JS

$scope.recordLocation = function() {
    $scope.locations.unshift({
        CountryName: $scope.country.CountryName,
        StateName: $scope.state.StateName,
        CityName: $scope.city.CityName
    });
    $scope.city = {};
    $scope.state = {};
    $scope.country = [];
};

寻求有关如何解决此问题的帮助。

谢谢

【问题讨论】:

    标签: angularjs angularjs-ng-repeat nested-forms angularjs-ng-model


    【解决方案1】:

    您的对象范围本质上存在问题。 (在这里阅读更多:https://docs.angularjs.org/guide/scope

    在您的 ng-repeat 中,为每个块创建一个子范围,该范围创建它自己的副本/引用在 ng-model 中指定的变量除非您将点模型用于模型属性(在使用 ng-model 时强烈推荐)。

    例如,而不是 $scope.city = ... 和在你的 html 中 ng-model="city" 你应该这样做:

    $scope.selection = {
      city: "",
      state: "",
      country: ""
    }
    

    和你的 html 你会做ng-model="selection.city"

    这将解决您的一个问题,但您可能仍会面临一些其他问题,因为您正在与主选择表单共享状态(或者您可能会接受这种行为)。我不确定您希望和/或计划如何在用户做出选择后更新“已编辑”位置,所以我不想提供更多建议,但将 ng-model 包含在其中可能是有意义的该表使用location(所以ng-model="location.city")中的值,但是您还必须更改更新后续下拉值的方式。

    【讨论】:

    • 我理解您所指的关于创建新范围以容纳所选对象的概念。话虽如此,在选择已添加到列表/表格并需要编辑后,您如何引用原始来源?
    • 这就是我正在努力解决的部分,是的,我已经(多次)引用了您在上面共享的链接。 AngularJS 文档只涉及表面,并没有提供大量真实世界的示例。这就是我在这里的原因:D
    • 另外,感谢您@JoseM 的回复!
    • 您说“话虽如此,在选择已添加到列表/表格并需要编辑后,您如何引用原始来源?” - 我已经回答了这个问题,ng-model="location.city" - 我的回答没有回答你最初提出的问题吗?
    • 我对 plunker 进行了另一次更新,显示两种表单都在工作,彼此分开next.plnkr.co/edit/3xmo9NMdw5qKvy0c
    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多