以编程方式更改 md-select 中的选定项目 [英] change selected item in md-select programmatically

查看:22
本文介绍了以编程方式更改 md-select 中的选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想最初(在页面加载时)在 md-select 中设置当前 schoolclass 项目.

I want to initially (on page load) set the current schoolclass item in the md-select.

这段代码设置了选中的schoolclass,但是没有在md-select中直观的体现出来.

This code sets the selected schoolclass but it is not reflected in the md-select visually.

$scope.selectedSchoolclass = schoolclasses.length > 0 ? schoolclasses[0] : null;

<md-input-container>
    <label>Schoolclasses</label>
    <md-select ng-model="selectedSchoolclass"  md-on-close="getPupils()">
        <md-option ng-repeat="s in schoolclasses" ng-value="{{s}}">
            {{s.schoolclassNumber}}
        </md-option>
    </md-select>
</md-input-container>

当我使用 md-selected="$first" 时,它在 md-select 中被视觉选中,但我从计算属性中得到一个错误,它似乎在 $first 之前先评估.因此,我无法将第一个学校班级设置为来自 html 的选定学校班级.我必须通过控制器中的 JS 来完成.

When I use md-selected="$first" then it is selected visually in the md-select but I get an error from a computed property which seem to evaluate first just before the $first. Thus I cannot set the first schoolclass as the selected schoolclass from html. I have to do it via JS in the controller.

我该怎么做?

推荐答案

你已经接近了.改变一下

You were close. Just change

<md-option ng-repeat="s in schoolclasses" ng-value="{{s}}">

<md-option ng-repeat="s in schoolclasses" ng-value="s">

ng-value 已经在期待一个角度表达式(见这里),所以你不需要用 {{}} 来计算表达式.

ng-value is already expecting an angular expression (see here), so you don't need to evaluate the expression with {{}}.

而且,我假设你的意思是:

And also, I assume you meant:

$scope.selectedSchoolclass = $scope.schoolclasses.length > 0 ? $scope.schoolclasses[0] : null;

schoolclasses 必须是 $scope.schoolclasses 因为您在模板中引用了它.

schoolclasses needs to be $scope.schoolclasses because you reference it in your template.

这篇关于以编程方式更改 md-select 中的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆