【问题标题】:return value the ng-model of a select within the repeat返回值重复中选择的 ng-model
【发布时间】:2016-12-30 17:58:26
【问题描述】:

我有问题返回值ng-model="selectStates" 在重复中选择!

在此评论的地方,是另一次尝试。

<div ng-controller="coursesCtrl">

    <form name="formFilters" id="formFilters">
        <div ng-repeat="filter in filters">
            <div ng-switch="filter.type">
                <div ng-switch-when="dropdown">
                    <h5 class="menu-filter-sidebars--title">{{filter.title}}</h5>

                        <select name="selectStates" id="selectStates" ng-options="option.name for option in filter.inputs" **`ng-model="selectStates"`** >
                         <!--  <option value="" > Selecione o Estado </option>
                          <option data-ng-repeat="item in filter.inputs" value="{{item.uf}}">{{item.Estado+' - '+item.uf}}</option> -->
                        </select>

                </div>
            </div>
        </div>
     </form> 

     <div class="test" > {{selectStates}} </div>

</div>

【问题讨论】:

  • 分享您的 JSON 对象以轻松调试

标签: angularjs angularjs-scope angular-ngmodel


【解决方案1】:

将您的 ng-model="selectStates" 更改为 ng-model="filter.selectStates",这应该会有所帮助。

【讨论】:

    【解决方案2】:

    在你的ng-model 中总是有一个点

    范围继承通常很简单,您甚至不需要知道它正在发生...直到您尝试双向数据绑定(即,表单元素,ng-model) 从子范围内到在父范围上定义的原语(例如,数字、字符串、布尔值)。它不像大多数人期望的那样工作。发生的情况是子作用域获得了自己的属性,该属性隐藏/隐藏了同名的父属性。这不是 AngularJS 正在做的事情——这就是 JavaScript 原型继承的工作方式。新的 AngularJS 开发人员通常没有意识到 ng-repeatng-switchng-viewng-include 都创建了新的子作用域,因此当涉及这些指令时,问题经常出现。 (有关问题的快速说明,请参阅this PLNKR。)

    遵循 always have a '.' in your ng-models 的“最佳实践”可以轻松避免原语的这个问题 - 观看 3 分钟。 Misko 演示了 ng-switch 的原始绑定问题。

    有一个“。”在您的模型中将确保原型继承发挥作用。所以,使用

    <input type="text" ng-model="someObj.prop1"> rather than 
    <input type="text" ng-model="prop1">.
    

    -- AngularJS Wiki -- Understanding Scopes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2017-01-31
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多