以角度js将数据发送到多个数组

时间:2016-03-14 13:36:02

标签: javascript angularjs

enter image description here我想在提及中发送更改值“false” - >“rcs” - >在json中“可见”到多个数组中 我是角色的新人,我不知道如何管理,所以尝试,但它不起作用。你能救我吗?

在我的json数组中有一个名为“mentions”的对象,在这个对象中有一个带有另一个名为“rcs”的对象的数组(在文件的末尾),并且有一行“visible”是一个布尔值。我想将此布尔值更改为“true”,因为它当前是“false”

这是我的json:

 "data": {
    "company_name": "Laplagem",
    "entrepreneur_firstname": "guillaumO",
    "entrepreneur_lastname": "nouveaunomF",
    "login": "gui.nouhaud@gmail.com",
    "company_address": "120 rue de la pacrette fraiche",
    "company_zipcode": "78630",
    "company_city": "Pignons sur rue",
    "company_country": "France",
    "company_phone": "0123435522",
    "activity_type": "V",
    "declaration_period": "M",
    "activity_start": "2016-02-01",
    "company_siret": "53106089500020",
    "rcs_city": "Paris",
    "naf_code": "",
    "has_accre": false,
    "invoice_header": "Laplagem\nguillaumO nouveaunomF\n120 rue de la pacrette fraiche\n78630 Pignons sur rue\ngui.nouhaud@gmail.com\n0123435522",
    "invoice_footer": "SIRET : 53106089500020 - RCS : Paris",
    "invoice_payment_delay": "60",
    "bank_balance": 0,
    "mentions": {
      "vat": {
        "value": "TVA non applicable, art. 293 B du CGI",
        "visible": true,
        "alwaysVisible": true,
        "alterable": false
      },
      "delay": {
        "value": "En cas de retard de paiement, une pénalité de 3 fois le taux d’intérêt légal sera appliquée, à laquelle s’ajoutera une indemnité forfaitaire pour frais de recouvrement de 40€",
        "visible": true,
        "alwaysVisible": true,
        "alterable": true
      },
      "free": {
        "value": "Ma nouvelle mention perso",
        "visible": true,
        "alwaysVisible": false,
        "alterable": true
      },
      "bank": {
        "value": "",
        "visible": true,
        "alwaysVisible": false,
        "alterable": true
      },
      "cga": {
        "value": "Membre d’une association agréée, le règlement des honoraires par chèque est accepté",
        "visible": true,
        "alwaysVisible": false,
        "alterable": true
      },
      "rcs": {
        "value": "Dispensé d’immatriculation au registre du commerce et des sociétés (RCS)",
        "visible": false,
        "alwaysVisible": false,
        "alterable": true
      }
    }
  }.

这是我的控制器:

$scope.valuercs = function(){
  var mytoken = sessionStorage.getItem('token');
  var transform = {"mentions":{"rcs": {"value" : "true"}} };
  factovalrcs.send(mytoken, transform).then(function(conf){
    console.log(conf);
  });
}

我的服务:

.factory('factovalrcs', ['$http','$q', function ($http,$q){
      var newdata = {};
      newdata.send = function(mytoken, transform){
       var deferred = $q.defer();
       $http({
           method: 'POST',
           url: "http://api.tiime-ae.fr/0.1/request/settings-update.php",
           headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           transformRequest: function(obj) {
               var str = [];
               for(var p in obj)
               str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
               return str.join("&");
           },
           data: {token: mytoken, transform}
           })
           .success(function(conf){

              deferred.resolve(conf);

              // var promise = deferred.promise;
              // promise.then(function(result){
              // var  mydata = result["data"];
              // console.log(mydata);
              //   }
              //);
            });
            return deferred.promise;
           };
           return newdata;
}])

1 个答案:

答案 0 :(得分:1)

您不能简单地分配给$scope.valuercs,因为您的AJAX调用是异步的。您需要在成功函数中切换visible位,并将结果分配给$scope.valuercs

  var mytoken = sessionStorage.getItem('token');
  var transform = {"mentions":{"rcs": {"value" : "true"}} };
  factovalrcs.send(mytoken, transform).then(function(conf){
    console.log(conf);
    conf.data.mentions.rcs.visible = true;
    console.log(conf);
    $scope.valuercs = conf;
  });