【问题标题】:Submit and edit value in the same form in AngularJS在 AngularJS 中以相同的形式提交和编辑值
【发布时间】:2014-06-21 12:07:37
【问题描述】:

我刚刚开始使用 AngularJS。 我想以相同的形式发送编辑值。 这是我的方法:

  • 我使用 ng-init 在输入范围内填充 array
  • fields 范围内,当我单击提交按钮时,我会存储新值。

但它似乎不起作用......我不明白为什么。

index.html

 <form class="form" name="myForm" novalidate>

    <div class="col span12 input"><input type="text" ng-model="fields.name" ng-init="array.name" required /></div>

    <div class="col span12 input"><input type="text" ng-model="fields.age" ng-init="array.age" required /></div>

    <select ng-model="fields.po_id" ng-init="fields.po_id" ng-options="productOwner in productOwners" required></select>

ng 添加

 </form>

controllers.js

$scope.productOwners =
[
    {
        "id": 1
    },
    {
        "id": 2
    },
    {
        "id": 3
    },
    {
        "id": 4
    },
];

$scope.array =
[
    {
        "name": "Hello"
    },
    {
        "age": "18"
    },
    {
        "po_id": "2"
    }
]; 

$scope.submit = function(fields){

}

【问题讨论】:

    标签: json forms angularjs angularjs-scope


    【解决方案1】:

    我创建了一个working Plunker 来解决您的问题。

    您的代码中有两个问题:

    1) ng-init="array.age" 应该是 ng-init="fields.age=array.age" 和另一个 ng-init 相同。 您没有保存 fields 中的值,因此它没有更新。

    2) 你的数组定义有问题。

    您应该将数组定义为:

    $scope.array = {
            "name": "Hello",
            "age": "18"
        }; 
    

    或者像这样在 HTML 模板中更改对数组的调用:

    array.name -> array[0].name

    array.age -> array[1].age


    编辑:

    Updated Plunker

    ng-options的正确用法是

    for array data sources:
       label for value in array
       select as label for value in array
       label group by group for value in array
       select as label group by group for value in array
    for object data sources:
       label for (key , value) in object
       select as label for (key , value) in object
       label group by group for (key, value) in object
       select as label group by group for (key, value) in object
    

    所以我已将您的 ng-options 从 ng-options="productOwner in productOwners" 更改为 ng-options="productOwner.id for productOwner in productOwners",其类型为:label for value in array,其中 labelproductOwner.id,值是对象 productOwner

    此外,ng-init 不需要,因为fields.po_id 在初始化时没有任何值。

    但是如果你想初始化值,你可以这样做:

    ng-init ="fields.po_id = productOwners[0]"
    

    【讨论】:

    • 哦!谢谢,它似乎工作。你能用 select 检查我上次的编辑吗?选择没有预先选择该选项。它总是空的......
    • 是的,但是fields.po_id 在初始化时应该有一个默认值。它必须具有来自"po_id": "2" 的值。预先选择的值是2,但我想用ng-options="productOwner in productOwners"显示其他选项
    • 我刚试过初始化=&lt;select ng-model="field.po_id" ng-init="field.po_id = 1"&gt;&lt;/select&gt;它不起作用:(
    • 1 不包含在数组 productOwners 中。如果要初始化该值,则必须传递一个对象,例如 ng-init ="fields.po_id = productOwners[0]" 或将 ng-options 更改为 ng-options="productOwner.id as productOwner.id for productOwner in productOwners",但您将无法检索产品所有者中链接的其他信息
    • 最好将ng-init中的表达式移动到控制器中。请参阅 Angular 网站上对 ng-init 的描述,该描述建议“ngInit 的唯一适当用途是用于别名 ngRepeat 的特殊属性”
    猜你喜欢
    • 2017-01-21
    • 2021-12-19
    • 2017-04-19
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多