Angularjs error issue - angularjs

I am getting an error TypeError: Cannot read property 'get' of undefined.i have tried so many ways but not able to solve.
mycode is login.controller.js
(function () {
'use strict';
angular
.module('app')
.controller('LoginController', LoginController);
LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
function LoginController($location, AuthenticationService, FlashService) {
var vm = this;
vm.login = login;
(function initController() {
// reset login status
AuthenticationService.ClearCredentials();
})();
function login() {
var usename=vm.username;
var password=vm.password;
vm.dataLoading = true;
$http.get('http://localhost:8080/ProjectManagement/REST/Login/Check?usename='+usename+'&password='+password+'').success(function(data, status, headers, config,response){
if (data=="0")
{
}
else
{
}
}).error(function(data, status, headers, config,response) {
});
};
}
})();

You missed to add $http dependency inside you controller, make sure all the dependencies are injected before using it.
Code
LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService', '$http'];
function LoginController($location, AuthenticationService, FlashService, $http) {

Related

Why angularjs's constant not working with factory

I have constant mainApp.constant('baseUrl', 'http://localhost:63760') , i need to use it as default base url for all of my factory. when I inject it to a controller, it's working perfectly, but not working with factory(when factory called by the controller) giving an undefined error. please see my below codes.
app.js
'use strict';
var mainApp = angular.module("mainApp", ["ui.router", "ui.bootstrap"]);
mainApp.constant('baseUrl', 'http://localhost:63760');
factory
mainApp.factory('StudentServices', ['$http', '$rootScope', function ($http, $rootScope, baseUrl) {
return {
GetStudentProfileByPIN: function (param) {
alert(baseUrl);
return $http({
url: baseUrl + '/Api/ApiProfile/GetStudentProfile/' + param,
method: 'POST',
async: false
});
}
};
}]);
controller
mainApp.controller('loginController', function ($scope, $rootScope, $state, $window, StudentServices) {
$scope.user = {};
$scope.login = function () {
StudentServices.GetStudentProfileByPIN($scope.PIN).then(function (response) {
if (response.data.Student != null) {
$window.localStorage.setItem('Student', angular.toJson(response.data));
$window.location.href = "/Profile/#/home";
}
else {
alert(response.data.Message);
}
});
};
});
You are missing **'baseUrl',**You should have it as,
mainApp.factory('StudentServices', ['$http', '$rootScope','baseUrl', function ($http, $rootScope, baseUrl) {
inject the string baseUrl too
mainApp.factory('StudentServices', ['$http', '$rootScope','baseUrl' function ($http, $rootScope, baseUrl)

call a soap webservice from ionic

i'm trying to call a soap webservice using this method : http://mcgivery.com/soap-web-services-angular-ionic/ however my screen only shows a blank page instead of the login screen. what could be the probleim?
here's my services.js code:
.factory('loginService', ['$soap', function($soap){
var base_url = 'http://localhost/UserService3/WebService1.asmx';
return {
GetUser: function(uname){
return $soap.post(base_url,"getUserbyUsername", {Username: uname});
}
};
}])
and here's my controller.js code:
.controller('loginCtrl', ['$scope', '$stateParams', '$location', '$state', 'sharedProperties2','ConnectivityMonitor', '$ionicLoading', 'loginService',
function ($scope, $stateParams, $location, $state, sharedProperties2,
ConnectivityMonitor, $ionicLoading, loginService) {
$scope.show = function() {
$ionicLoading.show({
template: '<ion-spinner icon="android"></ion-spinner>',
showBackdrop: false
});
};
$scope.hide = function(){
$ionicLoading.hide();
};
$scope.userdata = {}
$scope.enterlogin = function(usern,pass)
{
userFactory.GetUser(usern).then(function(response) {
alert('get = ' + JSON.stringify(response.data));
})
}
}])
apparently it could not see the previous location of the added scripts angular.soap.js and soapclient.js, so i put the 2 files inside a folder named angular-soap inside the lib folder.

Angular-Jasmine injecting a service into the test

New to Jasmine, I am trying to instantiate my controller which has a list of dependencies (mainly the services I have written) and all the different ways I';ve tried haven't been right.
Here is my controller:
(function () {
'use strict';
angular.module('app.match')
.controller('MatchController', MatchController);
MatchController.$inject = ['APP_CONFIG', '$authUser', '$http', '$rootScope', '$state', '$stateParams', 'SearchService', 'ConfirmMatchService', 'MusicOpsService', 'ContentOpsService', 'MatchstickService', 'MatchService', 'Restangular'];
function MatchController(APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, searchService, confirmMatchService, musicOpsService, contentOpsService, matchstickService, matchService, Restangular) {
var vm = this;
vm.greeting = '';
.
.
)();
Here is my test
(function(){
'use strict';
describe('app module', function() {
var MatchController;
//beforeEach(module('app.match'));
beforeEach(function($provide) {
module = angular.module('app.match');
$provide.service('SearchService', function(){
});
});
beforeEach(module('app.config'));
beforeEach(module('auth'));
beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams) {
MatchController = $controller('MatchController', {'APP_CONFIG':APP_CONFIG, '$authUser':$authUser, '$http':$http, '$rootScope':$rootScope, '$state':$state, '$stateParams':$stateParams, '$provide':$provide});
}));
describe("Match controller", function() {
it("should be created successfully", function() {
expect(MatchController).toBeDefined();
});
});
});
})();
Running test the above way gives me the following error:
TypeError: 'undefined' is not a function (evaluating '$provide.service('SearchService', function(){
})')
Try injecting the SearchService like this instead of using beforeEach.
describe('app module', function() {
var MatchController, SearchService;
beforeEach(module('app.match'));
beforeEach(module('app.config'));
beforeEach(module('auth'));
beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, _SearchService_) {
SearchService = _SearchService_;
MatchController = $controller('MatchController', {
'APP_CONFIG':APP_CONFIG,
'$authUser':$authUser,
'$http':$http,
'$rootScope':$rootScope,
'$state':$state,
'$stateParams':$stateParams,
'$provide':$provide,
'SearchService': _SearchService_
});
}));
describe("Match controller", function() {
it("should be created successfully", function() {
expect(MatchController).toBeDefined();
});
});
});
})();
Similarly, you'll have to inject other services as well that your controller is relying on.

Can't find out why i'm getting an injector error

I am getting an error on injecting my collections factory, but can't figure out where I went wrong:
Error: [$injector:unpr] http://errors.angularjs.org/1.3.0/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20collections
'use strict';
var thangular = angular.module('thangular', ['ngAnimate']);
thangular.config(function ($interpolateProvider,$httpProvider) {
$interpolateProvider.startSymbol('[[').endSymbol(']]');
$httpProvider.defaults.useXDomain = true;
});
thangular.factory('collections', ['$scope', '$http', '$q',
function ($scope, $http, $q) {
return {
all: function () {
var deferred = $q.defer();
var request = $http({
method: 'GET',
url: '/collections.json',
});
request
.success(function (result) {
deferred.resolve(result.content);
})
.error(function (error) {
deferred.reject(error);
});
return deferred.promise;
}
};
}
]);
thangular.controller('mainCtrl', ['$scope', 'collections',
function ($scope, collections) {
collections.all().then(function (data) {
console.log(data);
});
}
]);
I suppose you shouldn't inject $scope in factory declaration. Just change
thangular.factory('collections', ['$scope', '$http', '$q',
to
thangular.factory('collections', ['$http', '$q',
Factory declaration should not dependent upon controllers $scope.

Can't seem to be able to pass data from one controller to another

The issue is that I can't seem to send information from Controller 1 to Controller 2... I have my service that sets/gets data but it isn't working. The actual error that I'm getting is that Controller 1's dataService.getData is not a function... when it works elsewhere.
Service 1 (in its own file)
app.service('dataService', function() {
var data, queried;
return {
setData: function(queryData) {
this.data = queryData;
this.queried = false;
},
getData: function() {
this.queried = true;
return this.data;
}
};
});
Controller 1 (sending information)
app.controller('MyCtrl', ['$scope', '$location', '$state', function($scope, $location, $state, dataService) {
anotherService.functionName(function(err, data) {
// do some things here
actualService.doesntWork(function(err, data) {
if (!err) {
var query = {};
query.someField = data.someField;
dataService.setData(query);
$state.go("go.somewhere.else");
}
});
});
}]);
Controller 2 (getting information)
app.controller('MyCtrl2', ['$scope', '$location', '$state', function($scope, $location, $state, dataService) {
$scope.buttonPressed = function() {
console.log(dataService.getData());
}
}]);
You didn't injected service dataService inside your MyCtrl & MyCtrl2, ensure dependency should be injected before using it.
Controller
app.controller('MyCtrl', ['$scope', '$location', '$state','dataService', //<-added dependency here
function($scope, $location, $state, dataService) {
anotherService.functionName(function(err, data) {
// do some things here
actualService.doesntWork(function(err, data) {
if (!err) {
var query = {};
query.someField = data.someField;
dataService.setData(query);
$state.go("go.somewhere.else");
}
});
});
}]);
Controller2
app.controller('MyCtrl2', ['$scope', '$location', '$state','dataService',//<-added dependency here
function($scope, $location, $state, dataService) {
$scope.buttonPressed = function() {
console.log(dataService.getData());
}
}]);

Resources