I already saw this thread but it uses ui-router and I'm only using the $routeProvider of AngularJS. The code in the thread is:
.controller('SeeYouSoonCtrl', ['$scope', '$state', '$timeout',
function($scope, $state, $timeout) {
$timeout(function() {
$state.go('AnotherState');
}, 3000);
}])
How can I use it with my routeProvider since I am not using ui-router? Thank you in advance.
You need to use $location service
$location.path('/anotherURL');
By $location method :
code:
.controller('HomeController', ['$scope', '$state', '$location',
function($scope, $state, $location) {
$location.path('/appurl');
}])
Related
I am trying to load controllers dynamically using ocLazyLoad :
$ocLazyLoad.load('./ctrls/login.js');
But am getting this error saying:
The controller with the name 'loginCtrl' is not registered.
angular.module('optimusApp')
.controller('loginCtrl', function ($scope, $rootScope, $location, $http) {});
app.js
angular.module("optimusApp", ['ngRoute', 'oc.lazyLoad']);
angular.module('optimusApp')
.controller('globalCtrl', function ($rootScope, $location, $http, $routeParams, $ocLazyLoad) {
$ocLazyLoad.load('./ctrls/login.js');
});
I made it work by using ng-if
app.js
var optimusApp = angular.module("optimusApp", ["ngRoute", "oc.lazyLoad"])
.controller("globalCtrl", function ($rootScope, $location, $http, $routeParams, $ocLazyLoad) {
$ocLazyLoad.load("./js/app/login.js").then(function() {
console.log("loginCtrl loaded");
$rootScope.loginActive = true;
}, function(e) {
console.log("error");
});
});
login.js
optimusApp.controller('loginCtrl', function ($scope, $rootScope, $location, $http) {
});
HTML
<div ng-if="loginActive" ng-controller="loginCtrl">
</div>
Basically you will get the "registration error" if your ng-controller is in the page before you have the JS loaded.
So the ng-if is a solution, or I see you have ngRoute. So you could set ocLazyLoad to load the controller when entering a specific state.
I want to AngularJS Change Path Without Reloading, look http://joelsaupe.com/programming/angularjs-change-path-without-reloading/
in core.js:
'use strict';
angular.module('App',['ngRoute'])
.run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) {
var original = $location.path;
$location.path = function (path, reload) {
if (reload === false) {
var lastRoute = $route.current;
var un = $rootScope.$on('$locationChangeSuccess', function () {
$route.current = lastRoute;
un();
});
}
return original.apply($location, [path]);
};
}]);
In controller:
angular.module('App')
.controller('DetailController', ['$scope', '$location', function($scope) {
$scope.changeURL = function(){
console.log("IN changeURL");
$location.path('/sample/gfshdfdsf', false);
};
}]);
If invoke changeURL, it will occur error:ReferenceError: $location is not defined
Can somebody help me? Thanks!
$location is not injected in the controller, so just change
.controller('DetailController', ['$scope', '$location', function($scope)
to
.controller('DetailController', ['$scope', '$location', function($scope, $location)
I was getting the same error and I deleted the $rootScope from the definitions. After that it worked. No idea why.
Not working
app.factory("OrganizationService",
['$q', '$http', '$log', '$location', '$rootScope', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
function($scope , $http, $log, $location, $cookies, $rootScope, $timeout) {
Working
app.factory("OrganizationService",
['$q', '$http', '$log', '$location', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
function($scope , $http, $log, $location, $cookies, $timeout) {
I have a service and a controller declared like this:
angular
.module('purchaseApp')
.service('sharedProperties', function() {
var stringValue = 'test string value';
return {
getString: function() {
return stringValue;
}
}
})
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
When I try to access to the service in my controller, it returns me "undefined", but I don't know why... Could you help me?
Thanks.
Your controller is missing sharedProperties. You need to inject it. Add it after $http.
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', 'sharedProperties', function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
.controller('purchaseOrderCtrl', ['$scope', '$rootScope', '$location', '$http', **'sharedProperties'**, function ($scope, $rootScope, $location, $http, sharedProperties) {
console.log(sharedProperties.getString());
});
Add sharedProperties service in your controller dependent parameter as well.
I have built the next controller:
.controller('myCtrl', function($scope, $route, $routeParams, $location)
now I want to inject something that's called SharedState. tried to:
.controller('chatCtrl' ['SharedState', function($scope, $route, $routeParams, $location, SharedState)
or:
.controller('chatCtrl' ['$scope','SharedState', '$route', '$routeParams', '$location', function($scope, SharedState, $route, $routeParams, $location)
both returns error:
Error: [ng:areq] http://errors.angularjs.org/1.2.27/ng/areq?p0=myCtrl&p1=not%20aNaNunction%2C%20got%20undefined
what is the correct way to achieve injection?
my second try:
.controller('chatCtrl', ['$scope','SharedState', '$route', '$routeParams', '$location', function($scope, SharedState, $route, $routeParams, $location)
with comma works.
thanks for helping! #Aprillion
I am having the url as follows:
http://test.com/#/mya/classified/42/
How can I fetch 42 from this usl in angularjs?
Is there any way in angularjs $location to fetch the same?.
This is what $stateParams service is for:
myApp.controller('MyCtrl', ['$scope', '$stateParams',
function ($scope, $stateParams) {
console.log($stateParams.id);
}]);