为什么我不能改变Json的值

时间:2014-09-18 09:29:20

标签: javascript json angularjs

我想更新值“customer.signature”,但我的代码没有,我希望他做什么。 JSON和HTML没有错误,影响我想要的。 问题是,我的JS不工作。 “data.signature”正在更新。但“data.customer.signature”不是。

Json(我得到了休息“账户”):

{
  "signature": "newAccountSignatur",
  "signatureEnabled": true,
  "defaultMsisdn": "08282709909013",
  "httpForwardingAddress": "http://null.dev.to",
  "smtpForwardingAddress": "lazarus@null.dev.to",
  "customer": {
    "signature": "newCustomerSignatur",
    "id": 10339,
    "companyName": "Gerd Webapp Test v2.0.x",
    "diallingCodeId": 43,
  },
}

JS:

$scope.saveSignature = function () {
  if (AuthService.isAuth()) {
    Accounts.one().get().then(
        function (resultOk) {
          resultOk.data.customer.signature = $scope.newCustomerSig;
          resultOk.data.signature = $scope.newAccountSig;
          $log.d("resultOk: ", resultOk.data);
          resultOk.data.put().then(
              function (resultOk) {
                alert("Saved");
                $log.d("Accountinfo ok: ", resultOk);
                $scope.user = resultOk.data;
              },
              function (resultError) {
                $log.d("Accountinfo error: ", resultError);
                ErrorService.showApiError(resultError);
              }
          );
        }
    );
  }
};

HTML:

<form name="signature" ng-sub>
    <div>
      <textarea rows="5" cols="40" ng-model="newAccountSig" ng-trim="false" placeholder="{{user.signature}}"></textarea>
    </div>
    <i> Persönliche Signatur </i><br/>
    <div>
      <i> Zweites Beispiel</i><br/>
      <textarea rows="5" cols="40" ng-model="newCustomerSig" ng-trim="false" placeholder="{{user.customer.signature}}"></textarea>
    </div>
    <i>Information für mich: </i>
    <p>Aktueller Wert in account.signature = </p>
    <span>{{user.signature| stringIfBlank:'-'}}</span>
    <p>Aktueller Wert in account.customer.signature = </p>
    <span>{{user.customer.signature| stringIfBlank:'-'}}</span>
  </form>

2 个答案:

答案 0 :(得分:0)

您可以尝试以下示例:jsbin

var data ={
  "signature": "newAccountSignatur",
  "signatureEnabled": true,
  "defaultMsisdn": "08282709909013",
  "httpForwardingAddress": "http://null.dev.to",
  "smtpForwardingAddress": "lazarus@null.dev.to",
  "customer": {
    "signature": "newCustomerSignatur",
    "id": 10339,
    "companyName": "Gerd Webapp Test v2.0.x",
    "diallingCodeId": 43,
  },
};
data.customer.signature="newsign";
console.log(data.customer.signature);
alert(data.customer.signature);

答案 1 :(得分:0)

更新 嵌套的顺序是问题所在。对于那些可能需要处理熟悉问题的人。我不会删除它。

   $scope.saveSignature = function () {
      if (AuthService.isAuth()) {
        AccountCustomer.one().get().then(
            function (resultOk) {
              resultOk.data.signature = $scope.newCustomerSig;
              resultOk.data.put().then(
                  Accounts.one().get().then(
                  function (resultOk) {
                    resultOk.data.signature = $scope.newAccountSig;
                    resultOk.data.put().then(
                        function (resultOk) {
                          alert("Saved");
                          $log.d("Accountinfo ok: ", resultOk);
                          $scope.user = resultOk.data;
                        },
                        function (resultError) {
                          $log.d("Accountinfo error: ", resultError);
                          ErrorService.showApiError(resultError);
                        }
                    );
                  }
              ));
            }
        );
      }
    };