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

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

问题描述

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

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

此代码设置了所选的学校班级,但没有直观地反映在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 {{}}.

而且,我想你是说:

$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天全站免登陆