1

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>

4

2 回答 2

0

这是您问题的解决方案

$http({
    method: "get",
    url: "link to your json file"
}).success(function (data) {
     callback(data);
});
于 2015-11-26T07:45:16.063 回答
0

我没有看到使用的任何麻烦$http.get('file.json'),您可以angular.toJson(result)稍后使用将 JSON 字符串转换为对象。

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

于 2015-11-26T05:58:39.980 回答