在 angularjs 中使用选择绑定

Working with select binding in angularjs

本文关键字:选择 绑定 angularjs      更新时间:2023-09-26

我在这里用头撞墙,试图根据对象的属性绑定预定义的选择框的选定属性。

我有一个对象的集合:Items 和一个Item对象具有 StatusCode 属性,该属性为 0、1 或 2。

在我的标记上,我有以下内容:

<div ng-repeat="fb in feedbackItems | orderBy: 'Id'">
    <select>
        <option value="0">Some text</option>
        <option value="1">Some other text</option>
        <option value="2">Text 3</option>
    </select>
</div>

我需要的是检查我的fb对象的StatusCode是 0、1 还是 2,然后在右侧选项上设置 selected="selected" 属性。

当客户端选择另一个选项时,fb 对象也应更新。

我的控制器如下所示:

app.controller('feedbackController', function($scope, feedbackService, $filter) {
// Fields
var takeCount = 20;
var currentNodeId = $('.current-id').text();
// Constructor for this controller
init();
function init() {
    feedbackService.getFeedbackPaged(currentNodeId, 1, takeCount).then(function(response) {
        $scope.feedbackItems = response.data.Items;
        $scope.CurrentPage = response.data.CurrentPage + 1;
        $scope.TotalPages = response.data.TotalPages;
        $scope.TotalFeedbackItems = response.data.TotalItems;
        $scope.FeedbackCount = response.data.Items.length;
    });
}
});

有什么办法可以做到这一点吗? :-)

提前感谢!

确保将

模型放在那里。不确定您的模型是什么样子的,但根据我如何阅读您的问题:

<div ng-repeat="fb in feedbackItems | orderBy: 'Id'">
<select data-ng-model="fb.StatusCode"> <!-- here -->
    <option value="0">Some text</option>
    <option value="1">Some other text</option>
    <option value="2">Text 3</option>
</select>

或者如果反馈项实际上是您的选项列表(如 JBbird 所说)

<div >
<select data-ng-model="Item.StatusCode" ng-options="fb.id as fb.text for fb in feedbackItems"></select>
</div>