Issues with AngularJS $http-Service - javascript

The following code should fetch data from a MySQL Database with a $http-service but somehow the data, which the service provides doesn´t go to the controller/gets displayed wrong :/. If I start my application a empty or wrong table gets displayed at my page.
I provided some sampledata in the getUsers() function. The PHP code, which provides the data is working and this is a example of the JSON the PHP returns.
Maybe you guys can help me out to fix this up.
var app = angular.module('DBapp', []);
app.controller('MainCtrl', function($scope, $http, DatabaseService) {
$scope.userData = DatabaseService.getUsers();
});
app.factory('DatabaseService', function($http) {
var srv = {};
//Service Implementation
srv.getUsers = function() {
return [{"user_id":"1","firstname":"Barrack","lastname":"Obama","email":"obama#web.de"}, {"user_id":"2","firstname":"Tom","lastname":"Hanks","email":"tommy#web.de"},{"user_id":"6","firstname":"Felix","lastname":"Blume","email":"felix.b#selfmade.de"}];
};
//I provided some dummy-data in the getUsers-function as example output from the DB
//Public API
return {
getUsers: function() {
return srv.getUsers();
}
};
});
<!DOCTYPE html>
<html ng-app="DBapp">
<head>
<meta charset="ISO-8859-1">
<title>DB Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body ng-controller="MainCtrl">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div>
<div>
<div class="page-header">
<h1>Overview Database</h1>
</div>
<table style="font-size:20px; margin-left:10px;" class="table table-hover"">
<thead><tr><th>ID</th><th>Firstname</th><th>Secondname</th><th>Email</th></tr></thead>
<tbody>
<tr ng-repeat="user in userData">
<td ng-bind="user.user_id"></td>
<td ng-bind="user.firstname"></td>
<td ng-bind="user.lastname"></td>
<td ng-bind="user.email"></td>
<td>
<a ng-href="#/database/{{ user.user_id }}/edit"><button type="button" class="btn btn-info"><span class="glyphicon glyphicon-wrench"></span> Edit</button></a>
</td>
<td> <a ng-href="#/database/{{ user.user_id }}/delete"><button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button></a></td>
</tr>
</tbody>
</table>
<add-user-form></add-user-form>
</div>
</div>
</div>
</div>
<!-- SCRIPTS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src=lib/jquery/jquery-1.10.2.min.js></script>
<script src=lib/angular/angular.js></script>
<script src=lib/angular-route/angular-route.js></script>
<script src=scripts/app.js></script>
<script src=scripts/services/db_service.js></script>
<script src=scripts/controllers/main_controller.js></script>
<script src=scripts/directive/addUser_form.js></script>
<script src=scripts/controllers/delete_controller.js></script>
<script src=scripts/controllers/edit_controller.js></script>
</body>
</html>
As you can see above, it works with the dummy data. But if I change my service to the following code (with the actual $http-request) nothing works...
So the question is:
How do I pass my data from the http request to the $scope in the MainCtrl ?
app.factory('DatabaseService', function($http) {
var srv = {};
//Service Implementation
srv.getUsers = function() {
$http.post("php/getData.php").success(function(data) {
return data;
});
};
//Public API
return {
getUsers: function() {
return srv.getUsers();
}
};
});
`

It probably will work if you just return the array from your service like
return {
getUsers: function() {
return srv.getUsers;
}
};
The function call you used is not possible.
Ok here comes part 2:
I had no chance to test it, but it should work.
app.factory('DatabaseService', function($http) {
var srv = {};
//Service Implementation
$http.post("php/getData.php").success(function(data) {
srv.users = data;
});
//Public API
return {
getUsers: function() {
return srv.users;
}
};
});
Jorg is right, this part 2 was a hasty reaction, sorry. To return the promise is a possible solution, return $http.post("php/getDat.php"), and than in your controller add the success function
DatabaseService.getUsers().success(function(data) {
$scope.userData = data;
});
Hope that helps

Related

$http GET not showing in display in AngularJS

I am creating an app in AngularJS, where I am grabbing the data from the backend to display on the view. But, for some reason, I am getting the data in my console but not in my view. I will be so thankful if any one can help me solve this. Thanks in advance.
Here is my code. -Services
app.factory('user', ['$http', function($http) {
var userInfo = {
getUserInfo: function () {
return $http.get('https://************/*****/**/***********')
}
};
return userInfo;
}]);
home page(view)
<div class="users" ng-repeat="user in users | filter:userSearch" >
<a href="#/users/{{ user.id }}">
<img ng-src="{{user.img}}"/>
<span class="name">{{user.first}} </span>
<span class="name">{{user.last}} </span>
<p class="title">{{user.title}} </p>
<span class="date">{{user.date}} </span>
</a>
HomeController
var isConfirmed = false;
app.controller('HomeController', function($scope, user, $http) {
if (!isConfirmed) {
user.getUserInfo().then(function (response) {
$scope.userInfo = response.data;
isConfirmed = $scope.userInfo;
console.log($scope.userInfo);
}, function (error) {
console.log(error)
});
}
});
App.js
var app = angular.module("Portal", ['ngRoute']);
app.controller('MyCtrl', function($scope) {
$scope.inactive = true;
$scope.confirmedAction = function() {
isConfirmed.splice($scope.person.id, 1);
location.href = '#/users';
}
});
index.html
<!doctype html>
<html ng-app="Portal">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.28/angular-route.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="css/main.css" rel="stylesheet" />
</head>
<body>
<div class="header">
<div class="container">
<h3>Portal</h3>
</div>
</div>
<div class="main">
<div class="container">
<div ng-view>
</div>
</div>
</div>
<!-- Modules -->
<script src="js/app.js"></script>
<!-- Controllers -->
<script src="js/controllers/HomeController.js"></script>
<script src="js/controllers/UserController.js"></script>
<!-- Services -->
<script src="js/services/users.js"></script>
</body>
</html>
change your ng-repeat="user in users" to ng-repeat="user in userInfo"
as your assigning and consoling only $scope.userInfo in your controller
The property you assign data has to be same as of binded to view.
As per your HTML data should be in users. So do it like : $scope.users = response.data;.
or if you assign data to userInfo, then bind it on html. like :
<div class="users" ng-repeat="user in userInfo | filter:userSearch" >

How to call a function inside an Angular component triggered by jQuery events?

I have a third-party component that using jQuery (FineUploader). When a document gets uploaded, it fires an event in javascript/jquery and I need to, at that point, call a function that is inside my Angular component from the jquery code that is outside my Angular component.
The angular button works as expected but I can't get the jQuery button to call the function successfully.
Here's my plunker example code
HTML Page
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js#1.5.8" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script data-require="jquery#1.11.3" data-semver="1.11.3" src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<script>
function widgetController() {
var model = this;
model.addWidget = function(text) {
alert(text);
}
}
var app = angular.module("app", []);
app.component("widget", {
templateUrl: "template.html",
controllerAs: "model",
controller: [widgetController]
});
</script>
</head>
<body>
<div ng-app="app">
<widget></widget>
</div>
</body>
</html>
Template Page
<center>
<p>The jQuery button below is a representing a third-party component that can't be altered and uses jquery events.</p>
<button id="addAngular" ng-click='model.addWidget("ANGULAR WORKED!!")'>Add widget using Angular</button>
<button id="addJquery">Add widget using jQuery</button>
</center>
<script>
$(function() {
//This is mocking up an event from the third-party component.
$("#addJquery").on("click", function(){
model.addWidget("JQUERY WORKED!!"); //I need to be able to call the Angular function from jQuery
});
});
</script>
I think this may have done the trick but I'm not sure if there is a better answer. Please post an alternate solution if there is a better one.
Here's the working Plunker
HTML Page
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js#1.5.8" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script data-require="jquery#1.11.3" data-semver="1.11.3" src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<script>
var externalWidget;
function widgetController($scope, WidgetFactory) {
var model = this;
model.factory = WidgetFactory;
model.addWidget = function(text, isUpdatedExternally) {
model.isUpdated = text;
var newWidget = text;
model.factory.widgets.push(newWidget);
if(isUpdatedExternally)
{
$scope.$apply();
}
console.log("updated!");
}
model.checkWidget = function() {
console.log(model.isUpdated);
}
model.isUpdated = "NOT UPDATED";
model.addWidget("Controller initialized Widget");
externalWidget = model;
console.log(externalWidget);
}
var app = angular.module("app", []);
app.factory('WidgetFactory', function () {
var widgetList = [];
var initialWidget = "Factory Initialized Widget";
widgetList.push(initialWidget);
return {
widgets: widgetList
};
});
app.component("widget", {
templateUrl: "template.html",
controllerAs: "model",
controller: ["$scope", "WidgetFactory", widgetController]
});
</script>
</head>
<body>
<div ng-app="app" id="ngApp">
<widget></widget>
<br />
<center><button id="addJquery">Add widget using jQuery</button></center>
</div>
<script>
$(function() {
//This is mocking up an event from the third-party component.
$("#addJquery").on("click", function(){
externalWidget.addWidget("JQUERY WORKED!!!", true);
});
});
</script>
</body>
</html>
Template Page
<center>
<p>The jQuery button below is a representing a third-party component that can't be altered and uses jquery events.</p>
<button id="addAngular" ng-click='model.addWidget("ANGULAR WORKED!!")'>Add widget using Angular</button>
<button id="checkAngular" ng-click='model.checkWidget()'>Check widget using Angular</button>
</center>
<ul>
<li ng-repeat="w in model.factory.widgets">
{{w}}
</li>
</ul>

How to request API data with $http service

I'm developing an app with Riot Games API but this example it's done with REST Countries API. https://restcountries.eu/rest/v1/alpha/co
I use a MEAN.IO stack and this is my code:
test.html
<html ng-app="lolData">
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<section id="center" ng-controller="summonerStats">
<form ng-submit="search()">
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" id="summonerName" placeholder="Summoner Name">
<label class="mdl-textfield__label" for="sample1"></label>
</div>
<input class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" type="submit" value="Search"/>
<p ng-show="show">
{{ mystats.name }}
</p>
</form>
</section>
</body>
</html>
test.js
'use strict';
var lolData = angular.module('lolData', []);
console.log("before controller");
lolData.controller('summonerStats', function($scope, $http) {
var url = "https://restcountries.eu/rest/v1/alpha/co";
console.log("inside controller");
$scope.show = false;
$scope.search = function() {
$http.get(url)
.success(function(response) {
$scope.mystats = response;
$scope.show = true;
console.log("inside success controller");
});
};
});
When I refresh the page the code is executed until "before controller" console.log. It can't get inside lolData.controller. And in the browser console displays the following error.
And html doesn't accept the embeded javascript scope.
What am I missing?
Update 1:
I added the index.html in a codepen: Index.html
And Header.html in a codepen too: header.html
Not sure if this is your problem, but $http.success (and $http.error) has been depreciated since v1.4.4. Instead, use callback functions for success and error
var url = "https://restcountries.eu/rest/v1/alpha/co";
$http.get(url).then(
function successCallback(response) {
$scope.mystats = response;
$scope.show = true;
console.log("inside success controller");
}, function errorCallback(error) {
console.log(error);
});
Your response object doesn't do what you think it does.
From angular's docs you see the response has multiple properties attached to the returned object.
You want
$scope.mystats = JSON.parse(response.data);

pass param into a service function

// Code goes here
angular.module('app', [])
.controller('AController', function($scope, aService) {
$scope.isFinished = false;
$scope.$watch(function(){return aService.isFinished}, function(newVal, oldVal) {
$scope.isFinished = newVal;
});
})
.controller('BController', function($scope, aService, $timeout) {
$scope.load = function(){
$timeout(function() {
aService.isFinished = true; // how to pass extra param meter here?
}, 1000);
}
})
.service('aService', function() {
this.isFinished = false;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js#1.4.3" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body ng-app="app">
<div ng-controller="AController">
is finished? : {{isFinished}}
</div>
<div ng-controller="BController">
<button ng-click="load()">load of controller B</button>
</div>
<div ng-controller="CController">
<button ng-click="load()">load of controller C</button>
</div>
</body>
</html>
Above code demonstrate how I use service to talk between controllers. I just pass true if the value been changed in controllers. But now how to pass multiple param instead of just single true or false? so that I can know from which controller the value been changed.

Angualrjs factor is not working

I have this js
var app = angular.module('MyApp', []);
app.factory('Data', function () {
return {message: "I am data from a service"}
})
app.controller('FirstCtrl', function ($scope, Data) {
$scope.data = Data;
})
app.controller('SecondCtrl', function ($scope) {
})
and this is my html
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<title>index</title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/index.js"></script>
</head>
<body>
<div ng-controller="FirstCtrl" >
<input type="text" ng-model="message"/>
<h2>{{message}}</h2>
</div>
<div ng-controller="SecondCtrl" >
<input type="text" ng-model="message"/>
<h2>{{message}}</h2>
</div>
</body>
</html>
when I run the application, the message I am data from a service is not loaded into the h2 of the first controller although I made the factory and passed it to the controller
You are assigning $scope.data = Data. Then you should access it in the view
<div ng-controller="FirstCtrl" >
<input type="text" ng-model="data.message"/>
<h2>{{data.message}}</h2>
</div>

Categories