如何从json文件中读取数据,并在angularJS中以字符串或对象的形式返回



personManagerInstance.getString("firstname","common","en")当前我在ui中直接传递字符串,但我真正需要的是从json文件中读取数据并以字符串形式返回。。

personManagerInstance.getString("firstname","common","en")方法我需要从json文件中读取数据并以字符串或对象的形式返回?

personManagerInstance.getString("lastname","registration","en")这个基于参数的方法从不同的位置读取json并以字符串形式返回。。。

var PersonManager = function ()
{
  return {
    $get: function ($http, person)
    {
       var mainInfo = $http({
                              method: "get",
                              //"js/resources-locale_en_es.json"
                              url: "js/resources-locale_en_es.json"
                          }).success(function (data) {
                               return data.title;
                          });
      return {
        getPersonFirstName: function ()
        {
          return "as";
        },
        getPersonLastName: function ()
        {
          return person.lastName;
        },
        getString: function (labelid, moduleName, language)
        {
          //Here am getting the value id, moduleName, language based on the vaule I need to change the url path  
          //(i.e) js/resources-locale_en_es.json, js/registration/resources-locale_en_es.json
          
          var l = mainInfo.success(function (data) {
                               person.firstName = data.title;
                          })
          return person.firstName;
        }
      };
    }
  };
};
angular.module("mainModule", [])
  .value("person", {
    firstName: "",
    lastName: ""
  })
  .provider("personManager", PersonManager)
  .controller("mainController", function ($scope, person, personManager)
  {
    person.firstName = "John";
    person.lastName = "Doe";
    $scope.personInstance = person;
    $scope.personManagerInstance = personManager;
  });
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  <script src="script.js"></script>
</head>
<body ng-app="mainModule">
  <div ng-controller="mainController">
    <strong>First name:</strong> {{personManagerInstance.getPersonFirstName()}}<br />
    <strong>Last name:</strong> {{personManagerInstance.getPersonLastName()}}<br />
    <strong>Full name:</strong> {{personManagerInstance.getString("firstname",'common','en')}}  {{personManagerInstance.getString("lastname",'registration','en')}}<br />
    <br />
    <label>Set the first name: <input type="text" ng-model="personInstance.firstName"/></label><br />
    <label>Set the last name: <input type="text" ng-model="personInstance.lastName"/></label>
  </div>
</body>
</html>

我认为使用CCD_ 1没有任何问题,稍后可以使用angular.toJson(result)将JSON字符串转换为对象。

http://plnkr.co/edit/4K9sy5aCP4NML0nnYgZ4

这是问题的解决方案

$http({
    method: "get",
    url: "link to your json file"
}).success(function (data) {
     callback(data);
});

相关内容

  • 没有找到相关文章

最新更新