loading data from json files - javascript

Im trying to figure something out. If I want to load JSON data from a local file in my angular app, is there a way for me to include or to read the file in angular, or do I need to use some file API to read from the file and store the objects in a array. ty

you can use $http for loading json data from json file
var name = angular.module("app", []);
name.controller("ctrlu", ['$scope','$http', function($scope, $http)
{
$http.get('js/data.json').success (function(data){
$scope.guitarVariable = data;
});
}]
);
Note:you don't need to include(reference) json file in index.html page

Related

To use the json value declared in controller in module of that controller in angularjs

I have retrieved one json file in my login controller whose values I'm using in all the html files of my application but now I want to use the same JSON object in module.js files on all other pages.... can someone please suggest me how to fix this..This is the function which I've written in my controller, in which I want to use translation value in different page's module.js file.
Thanks in advance!!
function getTranslation(filname) {
var languageFilePath = 'app/pages/translations/translation_' + filname + '.json';
$resource(languageFilePath).get(function (data) {
$scope.translation = data;
});
}

Unable to retrieve JSON data using Ionic AngularJS framework

I'm doing Lynda's tutorial on Ionic framework and once I finally got onto some backend coding, I've run into this issue. Basically just generated the app and opened up the www/js/app.js file to add a controller to it with this code:
.controller('ListController', ['$scope', '$http', function($scope, $http){
$http.get('js/data.json').success(function(data){
$scope.artists = data;
});
}]);
www/js/data.json is the .json file with the data that I'm supposed to refer to in the index.html file using this code:
<ion-item ng-repeat='item in artists' class="item-thumbnail-left item-text-wrap">
<img src="img/{{item.shortname}}_tn.jpg" alt="{{item.name}} Photo">
<h2>{{item.name}}</h2>
<h3>{{item.reknown}}</h3>
<p>{{item.bio}}</p>
</ion-item>
But the retrieved data is null, as the img tag shows no value upon inspection, same as any other tag containing the 'item' data reference. What can I try?
you can try replacing data with below code:
$scope.artists = data.artists;
OR
$scope.artists = data.speakers;

Write textBox value in existing JSON file using AngularJS

Please help me to write textbox value / data into existing JSON file. I have tried with code below but unable with this. I am able to write in "textarea" but not in existing JSON file please let me know how to refer existing JSON file here and write data on that file.
var app = angular.module('myApp', []).controller('myCtrl', function ($scope) {
$scope.person = {};
$scope.getJSON = function () {
console.log("Creating a JSON");
$scope.jsonString = angular.toJson($scope.person, true);
};
});
My JSON file existing in dir: DataBase/DataBase.json
Most browsers don't allow javascript to access file system, most probably you would not be able to do this. I could suggest you to send your json data to a backend and rewrite file there.
Keep your json file inside mongodb or sqlite,then you can do all curd operation in the Json data

how can call json web services in angular js

Webservices url
http://3.srsm-s.appspot.com/trailerScreenOld.php?title=TWFhbiBLYXJhdGU=&_movie_id_=MjIlk4MTU3OA==&year=MjlmxNA==&seckey=g96c1n1m#
Js code
var web = angular.module('web',[]);
web.controller('webcont', function($scope,$http)
{
$http.get("http://3.srsm-s.appspot.com/trailerScreenOld.php?title=TWFhbiBLYXJhdGU=&_movie_id_=MjIlk4MTU3OA==&year=MjlmxNA==&seckey=g96c1n1m#").success(function(response)
{
$scope.names = response.movie;
}
);
});
As tried but the service is not loading and not displaying the data
it throws some error
such us
Page not found
request not initialized
I don't know so much about the service you connect but load the link in the browse to get the response on and see the result.
{"success":0,"message":"Oops SomeThing Went Wrong."}
Maybe that's the problem

Read a file from JQuery inside AngularJS

Hi I'm a beginner at programming. I'm trying to put data from a json file into a variable
I want to have AngularJS put text into a html document like this
{{ data.text }}
And the text is based off of a json file I have in the app.js, so I set data using JQuery:
myApp.controller('Ctrl', function ($scope, $http) {
$.getJSON("../static/file.json", function(data) {
$scope.data = data;
});
But nothing shows up initially. When I log $scope.data, it turns out to be undefined. But I know that my json file is correct because if I put all this code in some method that is called like
$scope.foo = function(){$.getJSON("../static/file.json", function(data) {
$scope.data = data;
});}
and have some button activate this, it'll work fine. But I want this to be there initially.
try
$.getJSON("/static/file.json", function(data) {
$scope.data = data;
});
then it load file from web http://xxx/static/file.json, not file system.

Categories