javascript - 从AngularJS中嵌套的ng-repeat内的下拉选项中获取选定的值

标签 javascript angularjs

我正在尝试访问下拉选项的选定值。但问题是我已经将 ng-repeat 作为父 div,并且下拉菜单中的选项也使用了 ng-repeat。所以基本上它是嵌套的 ng-repeat。

所以我无法访问选定的值。

jsFiddle

<div ng-controller="csrClrt">
<div  ng:repeat="item in items track by $index">

  <md-list one class="md-accordion">
            <md-input-container>
                   <label>Face Style:</label>
                  <md-select ng-model="style">
                   <md-option ng-value="user.name" ng:repeat="user in styles">{{user.name}}</md-option>
                  </md-select>
          </md-input-container>  
   </md-list>
</div>
<div>
<input type="button" value="get selected value" ng-click="getvalue()"/>
</div>

                    <p>
                    if i ttry out of parent ng repeat
                    </p>
                     <md-input-container>
                   <label>Face Style:</label>
                  <md-select ng-model="style">
                   <md-option ng-value="user.name" ng:repeat="user in styles">{{user.name}}</md-option>
                  </md-select>
          </md-input-container>
                              <div>

<input type="button" value="get selected value" ng-click="getvalue()"/>
                              </div>

                                 </div>

AngularJS

angular.module('MyApp', ['ngMaterial'])
.controller('csrClrt', function ($scope) {
     $scope.items=["0"];
     stylesdata=[{name:"a"},{name:"b"},{name:"c"}];
     var style=[];
     for(var i=0;i<stylesdata.length;i++)
     {
       style.push({
        name:stylesdata[i].name
       })
     }
     $scope.styles=style;
     $scope.getvalue=function()
     {

       alert($scope.style);
     }
})

最佳答案

如果您想在没有隔离范围的情况下执行此操作,则重复隔离范围,然后将变量绑定(bind)为对象而不是像这样的独立变量

<md-select ng-model="a.style">

并以

的方式访问它
 alert($scope.a.style);

参见 fiddle https://jsfiddle.net/44a0thqt/1/

关于javascript - 从AngularJS中嵌套的ng-repeat内的下拉选项中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973787/

相关文章:

javascript - WKWebView:页面加载后调用javascript

angularjs - 使用 jasmin 使用 $httpBackend 和 async/await 测试 angular.service

javascript - 根据文本输入显示和隐藏 DOM 元素

angularjs - Angular Bootstrap Modal 将背景打开

javascript - 使用 Firefox Addon (SDK) 保存网站图像

javascript - If 语句在 ie7 中不起作用

javascript - 以这些方式包装时,代码执行有何不同

javascript - jQuery .on 代表不工作

javascript - Angular : Is sharing a Controller between views a sign that Service is needed?

javascript - Angular 1.4 ES6 类指令