将所选语言从uib-dropdown语言选择器传递到$ scope

时间:2017-01-19 13:17:27

标签: javascript angularjs angular-ui-bootstrap angular-ui

我有下一个html模板,用户可以在其中选择语言:

 <div class="btn-group" uib-dropdown>
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" uib-dropdown-toggle>
                        <span class="lang-sm lang-lbl-full" lang="en"></span><span class="caret logincaret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu" uib-dropdown-menu >
                        <li><span class="lang-sm lang-lbl-full" lang="hu"></span></li>
                        <li><span class="lang-sm lang-lbl-full" lang="ru"></span></li>                        
                    </ul>
                </div>

如何将选定的选项lang属性绑定到控制器$ scope?

更新

理想的结果是here

1 个答案:

答案 0 :(得分:0)

我认为您想要使用简单的选择:

<select class="form-control" ng-model="selected" ng-options="o as o for o in ['hu', 'ru']"></select>

然后你会让它变得更简单并且模型被正确绑定(在$scope.selected中)

据我所知,下拉列表对于作为菜单的操作非常有用,而不是用于绑定选定的值。

或者,要继续使用下拉菜单,您需要自行管理价值更新(例如,使用ng点击下拉操作)

这样的事情:

<div class="btn-group uib-dropdown">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" uib-dropdown-toggle>
    <span class="lang-sm lang-lbl-full" lang="{{selected}}"></span> <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" uib-dropdown-menu >
    <li ng-repeat="lang in ['hu','ru']" ng-click="selected = lang"><span class="lang-sm lang-lbl-full" lang="{{lang}}"></span></li>
  </ul>
</div>