dropDown时使用ng-change获取所选对象

时间:2016-05-22 17:48:29

标签: javascript angularjs

{“id”形式的项目列表: some num ,“name”:某个名称}

通过viewBag获取

$scope.projects = @Html.Raw(Json.Encode(@ViewBag.vbProjects));

我的下拉菜单:

                <div class="form-group">
                <label class="col-md-1">Project</label>
                <div class="col-md-4">
                    <select class="form-control" ng-model="project" required>
                        <option ng-repeat="project in projects | orderBy : 'name'" value="{{project.id}}">{{project.name}}</option>
                    </select>
                </div>
            </div>
            project: {{project}}
            project.id: {{project.id}}
            project.name: {{project.name}}

我如何填写数据:

$scope.project = {"id":$scope.someObject.ProjectNameId ,"name":$scope.someObject.ProjectNameName};

结果:

  

project:{“id”:18,“name”:“someName”} project.id:18 project.name:   someName

然而在dropDown中,它会把它写给我。

我尝试使用此页面: getting the ng-object selected with ng-change

但没有帮助。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

尝试使用ng-options而非ng-repeat ......就像这样。

 <select class="form-control" ng-model="project" required
   ng-options="project as project.name for project in projects">
 </select>

详细了解select指令