angular.js更新模型

时间:2016-02-11 23:26:06

标签: javascript angularjs

如果您尝试下面的jsfiddle链接,请在标记为“标记名称”的文本框中键入内容,然后单击更新按钮。请注意console.log>正如预期的那样,ParmAndValue.Value设置为您键入的内容。但是现在输入其他内容并再次点击更新。这次是不是更新模型?怎么样?

https://jsfiddle.net/8evuoqLz/11/

<body ng-app="eApp">
<div id="sms" ng-controller="smsController" ng-cloak>
  <ul id="paramsList" class="ulDynamic">
    <li ng-repeat="actionParam in actions.SmsResponseActionParmAndValue" id="li_{{actionParam.Order}}">
      <div ng-repeat="param in actionParam.ParmAndValue">
        <label ng-bind="param.ParmName"></label>
        <input ng-model="param.Value" type="{{param.SmsInputType}}" />
      </div>
    </li>
  </ul>
<br>

         

的javascript

var app = angular.module('eApp', []);
  app.controller('smsController', function ($scope) {

  $scope.actions = {"SmsResponseActionParmAndValue":[{"Description":"Tag     User","Order":null,"ParmAndValue":[{"ParmId":9,"ParmName":"Tag Name","ParmType":"String","ParmVersion":"","Value":"","ValueId":null,"ValueVersion":"","ExtensionData":{},"Id":null,"Version":null}],"ExtensionData":{},"Id":10,"Version":"AAAAAAp96lg="}],"ExtensionData":{},"Id":null,"Version":null};

  $scope.updateDetails = function () {
    console.log("update");
    console.log($scope.actions.SmsResponseActionParmAndValue);
  };
});

3 个答案:

答案 0 :(得分:1)

您错过了SmsInputType的密钥ParmAndValue。现在每次Value更新。

"ParmAndValue": [{
    "ParmId": 9,
    "ParmName": "Tag Name",
    "ParmType": "String",
    "ParmVersion": "",
    "Value": "",
    "ValueId": null,
    "ValueVersion": "",
    "ExtensionData": {},
    "Id": null,
    "Version": null,
    "SmsInputType": "text" //<---- This one
  }],

以下是完整代码:

var app = angular.module('eApp', []);
//Controller
app.controller('smsController', function($scope) {

  $scope.actions = {
    "SmsResponseActionParmAndValue": [{
      "Description": "Tag User",
      "Order": null,
      "ParmAndValue": [{
        "ParmId": 9,
        "ParmName": "Tag Name",
        "ParmType": "String",
        "ParmVersion": "",
        "Value": "",
        "ValueId": null,
        "ValueVersion": "",
        "ExtensionData": {},
        "Id": null,
        "Version": null,
        "SmsInputType": "text"
      }],
      "ExtensionData": {},
      "Id": 10,
      "Version": "AAAAAAp96lg="
    }],
    "ExtensionData": {},
    "Id": null,
    "Version": null
  };

  $scope.updateDetails = function() {
    console.log("update");
    console.log($scope.actions.SmsResponseActionParmAndValue[0].ParmAndValue[0].Value);
    console.log($scope.actions.SmsResponseActionParmAndValue);
  };
});

答案 1 :(得分:1)

它也适合我。

您发布信息的可能原因:

1)浏览器的奇怪行为。

2)查看错误的数据&#34;凌乱&#34;结构

console.log($scope.actions.SmsResponseActionParmAndValue[0].ParmAndValue[0].Value);

答案 2 :(得分:0)

它为我更新了模型。我查看了代码,看起来很好。您确定没有在调试控制台中查看上一个对象而不是新对象吗?