'use strict';
(function () {
angular
.module('systemStatusApp')
.config(function ($stateProvider) {
$stateProvider
.state('app.isomicroservices', {
url: 'isomicroservices',
templateUrl: 'app/isomicroservices/templates/isomicroservices.html',
controller: 'isomicroservicesCtrl',
resolve : {sampledetails : function(){
console.log("resolve");
}
return "asdf";
}
});
});
})();
--
'use strict';
(function () {
angular
.module('isomicroservices')
.controller('isomicroservicesCtrl', 'sampledetails',function (sampledetails) {
console.log("sampledetails"+sampledetails);
});
})();
I think your code maybe can be:
'use strict';
(function () {
angular
.module('isomicroservices')
.controller('isomicroservicesCtrl', ['sampledetails', function (sampledetails) {
console.log("sampledetails"+sampledetails);
});
}])();
Related
I am unit testing a controller
USing karma.
Test case for controller:
'use strict';
fdescribe('products', function() {
// load the controller's module
var ctrl, scope;
beforeEach(angular.mock.module('products'));
beforeEach(function() {
module('products', function($controllerProvider) {
$controllerProvider.register('productsController', function($scope) {
});
});
});
describe('controller', function() {
beforeEach(function() {
angular.mock.inject(function($controller, $rootScope) {
scope = $rootScope.$new();
ctrl = $controller('productsController', {
$scope: scope
});
})
});
it('controller is set', function() {
expect(ctrl.currentCategory).toBe("ALL_PRODUCTS");
});
});
});
I have mocked the module in which the controller is there.
The expected code here is giving error as expect undefined to be "ALL_PRODUCTS".
controller.js:
(function() {
'use strict';
var productsController = function($rootScope, $scope) {
var vm = this;
vm.currentCategory = "ALL_PRODUCTS";
};
productsController.$inject = ['$rootScope', '$scope'];
module.exports = productsController;
})();
Module.js:
(function() {
'use strict';
module.exports = angular.module('products', ['common.services'])
.controller('productController', require('./controller.js'))
})();
Any help would be appreciated.
I'm trying to do a simple app with ionic framework to learn how to use Firebase and manipulate data. I did everything, but my console is showing nothing. I cannot run my controller. Does anyone know what is happening?
I injected everything, at least I think I did. I guess the error is in the resolve. Is there anyone that can save me with this, please?
Here's my Controller:
(function(){
'use strict';
angular
.module('hsAdmin.users')
.controller('UsersCtrl', UsersCtrl);
UsersCtrl.$inject = ['$scope', '$state','Users' ];
function UsersCtrl($scope, $state,Users ){
var vm = angular.extend(this, {
ChangeState: ChangeState,
Users:Users
});
function ChangeState(state){
$state.go(state);
}
}
})();
Here's my Module:
(function(){
'use strict';
angular
.module('hsAdmin.users', [
'ionic',
'ngCordova',
'hsAdmin.users'
])
.config(function($stateProvider) {
$stateProvider
.state('app.users', {
url: '/users',
views: {
'menuContent': {
templateUrl: 'templates/users/users.html',
controller: 'UsersCtrl as vm'
}
}
,resolve:{
Users: function(UsersService) {
return UsersService.GetUsers().$loaded().then(function(user){
return user;
}).catch(function(error){
console.log('Error when get users: ' + error);
})
}
}
});
})
})();
Here's my Service
(function(){
'use strict';
angular
.module('hsAdmin.users')
.factory('UsersService', UsersService);
UsersService.$inject = ['fb', '$firebaseArray', '$firebaseObject','$q','$rootScope'];
function UsersService(fb, $firebaseArray, $firebaseObject,$q,$rootScope){
var service = {
GetUsers:GetUsers,
GetUsersById:GetUsersById
};
return service;
function GetUsers(){
var query = fb.child('/users');
return $firebaseArray(query);
}
function GetUsersById(id){
var query = fb.child('/users/' + id);
return $firebaseObject(query).$loaded();
}
}
})();
You are creating your hsAdmin.users module and then injecting it into itself, that is likely what is causing your problem. Try the below:
(function(){
'use strict';
angular
.module('hsAdmin.users', [
'ionic',
'ngCordova',
// 'hsAdmin.users'
])
.config(function($stateProvider) {
$stateProvider
.state('app.users', {
url: '/users',
views: {
'menuContent': {
templateUrl: 'templates/users/users.html',
controller: 'UsersCtrl as vm'
}
}
,resolve:{
Users: function(UsersService) {
return UsersService.GetUsers().$loaded().then(function(user){
return user;
}).catch(function(error){
console.log('Error when get users: ' + error);
})
}
}
});
})
})();
Try to implement controller in following way.
(function() {
'use strict';
var UsersCtrl = function($scope, $state, Users) {
var vm = angular.extend(this, {
ChangeState: ChangeState,
Users: Users
});
function ChangeState(state) {
$state.go(state);
}
}
UsersCtrl.$inject = ['$scope', '$state', 'Users'];
angular.module('hsAdmin.users').controller('UsersCtrl', UsersCtrl);
})();
Why $location.url( "/messages" ); is not working? is there any restriction about $location inside a factory in AngularJS? any idea what is going on?
If I'm doing something wrong can you please tell my how should I redirect to another url "/messages"? I can't reload the page using so $window is not an option.
Thanks
My factory:
(function () {
'use strict';
angular
.module('app.conversation')
.factory('conversationFactory', conversation);
conversation.$inject = ['$rootScope', '$q', 'authFactory', '$location'];
function conversation($rootScope, $q, authFactory, $location) {
var service = {
changeLocation: changeLocation
};
return service;
function changeLocation() {
$location.url( "/messages" ); // recarga MIERDA!!!!!
}
}
})();
Route works fine:
(function () {
'use strict';
angular
.module('app.messages')
.config(configFunction);
configFunction.$inject = ['$routeProvider'];
function configFunction($routeProvider) {
$routeProvider
.when('/messages', {
templateUrl: 'app/messages/messages.html',
controller: 'messagesController',
controllerAs: 'meC',
resolve: {
user: function ($q, $timeout, authFactory) {
return authFactory.isLoggedInPromise();
}
}
}).when('/compose/:to?', {
templateUrl: 'app/messages/compose.html',
controller: 'composeController',
controllerAs: 'mcC',
resolve: {
user: function ($q, $timeout, authFactory) {
return authFactory.isLoggedInPromise();
}
}
});
}
})();
use $location.path( "/messages" );
I did:
$location.path( "/messages" ); // and then
$rootScope.$apply()
$location.path doesn't change in a factory with AngularJS
I have an Angular framework I am building that I am trying to make router agnostic, I would like to be able to use any router. So far I have tested it with ng router and it works fine, but when I try to use UI-Router I am getting the injector error. I am not sure if I have placed it in the wrong module, or if it a deeper issue. The framework broadcasts a route or a state, depending on how I set up the Framework directive. I have injected UI-Router into my main module and injected $stateProvider into my controller that needs to use it. I am stumped. Any help would be greatly appreciated.
Here is main module:
(function () {
"use strict";
angular.module("app", ["ptFramework", "ui.router", "ngStorage", "ui.bootstrap"]);
})();
Here is framework module:
(function () {
"use strict";
angular.module("ptFramework", [,"ptMenu", "ptDashboard"]);
})();
Here is framework controller:
(function () {
"use strict";
angular.module("ptFramework").controller("ptFrameworkController",
['$scope', '$window', '$timeout', '$rootScope', '$stateProvider',
function ($scope, $window, $timeout, $rootScope, $stateProvider) {
$scope.isMenuVisible = true;
$scope.isMenuButtonVisible = true;
$scope.isMenuVertical = true;
$scope.$on('pt-menu-item-selected-event', function (evt, data) {
$scope.stateString = data.state;
$stateProvider.go(data.state);
checkWidth();
broadcastMenuState();
});
$scope.$on('pt-menu-orientation-changed-event', function (evt, data) {
$scope.isMenuVertical = data.isMenuVertical;
$timeout(function () {
$($window).trigger('resize');
}, 0);
});
$($window).on('resize.ptFramework', function () {
$scope.$apply(function () {
checkWidth();
broadcastMenuState();
});
});
$scope.$on("$destroy", function () {
$($window).off("resize.ptFramework"); // remove the handler added earlier
});
var checkWidth = function () {
var width = Math.max($($window).width(), $window.innerWidth);
$scope.isMenuVisible = (width >= 768);
$scope.isMenuButtonVisible = !$scope.isMenuVisible;
};
$scope.menuButtonClicked = function () {
$scope.isMenuVisible = !$scope.isMenuVisible;
broadcastMenuState();
};
var broadcastMenuState = function () {
$rootScope.$broadcast('pt-menu-show',
{
show: $scope.isMenuVisible,
isVertical: $scope.isMenuVertical,
allowHorizontalToggle: !$scope.isMenuButtonVisible
});
};
$timeout(function () {
checkWidth();
}, 0);
}
]);
})();
As you can see I have injected $stateProvider in both the minsafe array and the function. I dont understand why I am getting this error
Here is the rote.config where I use it:
"use strict";
angular.module('app').config([
'$stateProvider', function ($stateProvider) {
$stateProvider
.state('dashboard',
{
url: "/dashboard",
template: "<h1>dashboard</h1>"
});
Any input would be greatly appreciated.
Thanks,
John.
In the controller:
$stateProvider.go(data.state);
should be
$state.go(data.state);
As a consequence, inject $state instead of $stateProvider.
I'm working on a Themeforest-Template,where the controllers looks like this:
(function () {
"use strict";
angular.module("app.tables", []).controller("articlesCtrl", ["$scope", "$http",
function ($scope, $http) {
$http.get('www/articles.php?method=fetchAll').success(function (data) {
$scope.articles = data;
console.log($scope.articles);
}).
error(function (data) {
// log error
})
}
])}.call(this))
Everywhere in the web I find guides and tutorials where the controllers looks like this:
app.factory('articlesFactory', function ($http, $resource) {
$http.get('www/articles.php?method=fetchAll').success(function (data) {
return $resource(data);
});
app.controller('articlesCtrl', function ($scope, articlesFactory) {
$scope.articles = articlesFactory.query();
})});
why does this not work for me ?
my app.js looks like this:
(function () {
'use strict';
angular.module('app', [
'ngRoute',
'ngAnimate',
'ui.bootstrap',
'easypiechart',
'textAngular',
'ui.tree',
'ngMap',
'ngTagsInput',
'app.controllers',
'app.directives',
'app.directives',
'app.localization',
'app.nav',
'app.ui.ctrls',
'app.ui.directives',
'app.ui.services',
'app.ui.map',
'app.form.validation',
'app.ui.form.ctrls',
'app.ui.form.directives',
'app.tables',
'app.task',
'app.chart.ctrls',
'app.chart.directives',
'app.page.ctrls',
'app.calls',
'app.avgLic'
]).config([
'$routeProvider', function ($routeProvider) {
var routes, setRoutes;
routes = [
"calls/calls",
"calls/archive",
"calls/statistics",
"calls/sms",
"contacts/contacts",
"configurator/configurator",
"configurator/offers",
"configurator/search",
"configurator/articles",
"configurator/orders",
"configurator/distributors",
"orders/new_orders",
"orders/archive",
"avg/active_lic",
"avg/new_lic"
];
setRoutes = function (route) {
var config, url;
url = '/' + route;
config = {
templateUrl: 'views/' + route + '.html'
};
$routeProvider.when(url, config);
return $routeProvider;
};
routes.forEach(function (route) {
return setRoutes(route);
});
return $routeProvider.when('/', {
redirectTo: '/calls/calls'
}).when('/404', {
templateUrl: 'views/pages/404.html'
}).otherwise({
redirectTo: '/404'
});
}
]);}).call(this);