简体   繁体   中英

How to read the data from json file and return as string or object in angularJS?

personManagerInstance.getString("firstname",'common','en') currently i pass direct string in ui its affecting but what i exactly need is read the data from json file and return as string..

personManagerInstance.getString("firstname",'common','en') method i need to read the data from json file and return as string or object?

personManagerInstance.getString("lastname",'registration','en') this method based on parameter read json from different location and return as string...

 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 //(ie) 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> 

I don't see any trouble of using $http.get('file.json') , you can use angular.toJson(result) to convert JSON string to object later on.

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

This is solution for your question

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM