【问题标题】:AngularJS select option dont pass value into http post requestAngularJS选择选项不将值传递给http post请求
【发布时间】:2017-01-23 06:03:21
【问题描述】:

我有选择框,我使用角度常数传递数据。我有两种不同的类型,因为我试图解决我的问题但没有成功。问题是我的选择框没有将任何值传递给发布请求,我不知道为什么。我也使用 ng-option 进行了尝试,但结果相同。

(function () {
app.constant("Constants", {
    Status: {
        1 : "New",
        2 : "Active",
        3 : "OnHold",
        4 : "Testing",
        5 : "Finished",
        6 : "Dropped"
    },
    Priority: [
        { id: "1", name: "Low" },
        { id: "2", name: "Medium" },
        { id: "3", name: "High" },
        { id: "4", name: "Urgent" },
    ],
});

在这里我将这些枚举绑定到我的角度控制器中的范围

    $scope.priority = {
        model: null,
        options: Constants.Priority,
    }
    $scope.status = {
        model: null,
        options: Constants.Status,
    }

这是我的观点,我将来自控制器的这些数据用于我的表单。我根据官方角度文档使用 ng-repeat:https://docs.angularjs.org/api/ng/directive/select

<div class="form-group">
    <label>Select Status</label>
    <select name="status_select" id="status_select" ng-model="status.model" class="form-control">
         <option ng-repeat="x in status.options" value="{{x.id}}">{{x}}</option>
    </select>
</div>

我错过了什么,为什么它不能正常工作?

【问题讨论】:

    标签: angularjs select


    【解决方案1】:

    您的 Status 是一个对象,您没有任何 id

    使用 ng-options

    <select ng-model="status.model" ng-options="key as value for (key , value) in status.options">
    </select>
    

    或像这样重复:

    <select ng-model="status.model">
        <option ng-repeat="(key, value) in status.options" value="{{key}}">{{value}}</option>
    </select>
    

    【讨论】:

    • 感谢您的快速响应,我试过了,但不幸的是它不适合我。它仍然会从这些选择框中发送除我的值之外的所有内容。我的代码中一定有一些愚蠢的错误,但我现在看不到。
    • @Martin 请为您的代码创建 plunker 以获得更多帮助
    • 我已经解决了。我犯了一个愚蠢的错误。我将选择框分配给错误的模型。我在安装并看到它的 ng-inspector 中检查了它。我不得不使用这个 ng-model
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多