2016-04-07 32 views
1

実際、私は1つのJSONスコープを持っています。 ng-modelが変更になるたびにJSON値を変更する必要があります。以下の例では、属性(CuId、名前、数量、レート、金額)を持つ行セットを持つJSONです。これらの属性名は、ng-model = CuIdのようなコントロール(テキストボックス)をバインドする必要があります。したがって、対応する属性値が変更されるたびに、JSONを更新する必要があります。JSONとの双方向バインディング?

これに関連する例を示したり、これを達成する方法を教えてください。

JSON:

$scope.postJSON = { 
     "entityInfo": { 
     "entity": "", 
     "tenantId": "", 
     "timeStamp": "2016-04-07T09:37:16.187Z" 
     }, 
     "collections": { 
     "Customer29Jan16": { 
      "meta": { 
      "parentreference": "***", 
      "pkname": "***", 
      "fkname": "***" 
      }, 
      "rowset": [ 
      { 
       "CuId": "test", 
       "Name": "test", 
       "Quantity": "test", 
       "Rate": "test", 
       "Amount": "test" 
      } 
      ], 
      "rowfilter": [] 
     } 
     } 
    } 
+0

確認しました。 angularには双方向のデータバインディングがあるため、モデルに加えた変更はjsonに反映されます。要素に正しくアクセスし、htmlのng-modelとして追加するだけです。 – v1shnu

+0

'ng-model =" postJSON.collections.rowset [0] .CuId "'のようなものを使用してください。 – senschen

+0

@ViChUはい私はチェックして、双方向バインドを使って小さな例を示しましたが、複雑な例が1つ必要です。 – bagya

答えて

4

他の回答に記載されているとおり、ツリー全体を変更するプロパティまで使用する必要があります。しかし、彼らはrowsetが配列なので、アクセスする要素をrowsetと指定しなければならないことには言及していませんでした。 ng-model="postJSON.collections.Customer29Jan16.rowset[0].CuId"のようなものを使用してください。

2

あなたはNG-モデルでツリー全体を配置する必要があります。

<input type="text" ng-model="postJSON.collections.Customer29Jan16.rowset.CuId" /> 
2
<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].CuId" /> 

や、物事をよりコンパクトにする、スコープ上の行セットを置く:このように、その後

$scope.rowset = $scope.postJSON.collections.Customer29Jan16.rowset[0]; 

使用:

<input ng-model="rowset.CuId" /> 
1

をあなたはpostDataを追加しましたスコープに追加すると、そうした個々の属性にバインドすることができます。

<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].CuId" /> 
<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].Name" /> 
<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].Quantity" /> 
<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].Rate" /> 
<input ng-model="postJSON.collections.Customer29Jan16.rowset[0].Amount" /> 
関連する問題