Angular Translate用于来自REST的数据

时间:2017-01-29 07:46:39

标签: angularjs angular-translate

我必须从REST服务转换数据。数据在数据库中,客户端想要这个。

在前端我有这个:

控制器:

CreateListService.getCharacteristic().then(function(response){
     vm.createListData = response.data;
});

查看:

<select 
    name="" 
    id="territory"
    class="form-control"
    data-ng-options="item.labelEn for item in vm.createListData.territory track by vm.createListData.territory" 
    ng-model="territory">       
</select>

JSON:

{  
   "confidentiality":[  
      {  
         "id":200,
         "labelFr":"Publique",
         "labelEn":"Public"
      },
      {  
         "id":201,
         "labelFr":"Restreinte",
         "labelEn":"Restricted"
      }
   ],
   "issuer":[  
      {  
         "id":600,
         "labelFr":"Federal Register - OFAC",
         "labelEn":"Federal Register - OFAC"
      },
      {  
         "id":601,
         "labelFr":"Ofac",
         "labelEn":"Internet Ofac"
      }
   ],
   "scope":[  
      {  
         "id":700,
         "labelFr":"Locale",
         "labelEn":"Local"
      },
      {  
         "id":701,
         "labelFr":"Groupe",
         "labelEn":"Group"
      }
   ]
}

时: Angular 1.4.9, Angular翻译:2.11.1

我的问题是当用户更改语言时,如何更改视图中的值(基于rest键:labelFr和labelEn)?

我在视图中尝试这样: item。{{labelLang}}用于vm.createListData.scope中的项目跟踪vm.createListData.scope

并在控制器中: $ rootScope.labelLang =&#34; label&#34; + capitalizeFirstLetter($ scope.selectedLang); 但是值没有更新。

感谢所有信息!

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

当您已经获得已翻译的标签时,您只需要在视图中输出它。 您在item[labelLang] for item in vm.createListData.scope track by vm.createListData.scope 中使用了错误的语法。 试试这个

public async Task<IActionResult> Create([Bind("ID,Name,Email,Job Title,ICollection<IFormFile> uploads, Track track)
      {
           if (ModelState.IsValid)
            {
                _context.Add(track);

                // Uploading files for the Request Database
                foreach (var upload in uploads)
                {
                    if (upload.Length > 0)
                    {
                        // Getting file into buffer.
                        byte[] buffer = null;
                        using (var stream = upload.OpenReadStream())
                        {
                            buffer = new byte[stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);
                        }
                        // Converting buffer into base64 code.
                        string base64FileRepresentation = Convert.ToBase64String(buffer);
                        // Saving it into database.
                        _context.Upload.Add(new Request()
                        {
                            UploadName = string.Format("{0}_{1}", DateTime.UtcNow.ToString("yyyyMMddHHmmss"), Request.FileName),
                            Uploadcode = base64FileRepresentation,
                            TrackID = track.ID,
                        });
                        await _context.SaveChangesAsync();

                    }
                }
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(track);
        }