Angular routing is not hitting its route - angularjs

Here i'm new to angularjs please help me why i'm not able to hit my routing. The problem is that when I click on the contact link it's not firing the .when function.
var app = angular.module('MyApp', ['ngRoute'])
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/Contact', {
templateUrl: '/ApiClient/Contact/AdminContact.html',
controller:'ContactCtrl'
})
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
hashPrefix:''
});
})
Html
<div>
Contact
</div>

You may try for this.
<p>Main</p>
Contact // Actually you put #!/Contact instead of #!Contact
<div ng-view></div>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/Contact", {
templateUrl: '/ApiClient/Contact/AdminContact.html',
controller:'ContactCtrl'
});
});

Should be
Contact
not
Contact

Related

How does angularjs routing work?

I have the following application config, the otherwise section seem to win all the time Why details are not matching here?:
var app = angular.module("atTheMovies", ["ngRoute"]);
var config = function ($routeProvider, $locationProvider) {
$routeProvider
.when('/list',
{ templateUrl: "/client/views/list.html" }
)
.when("/details/:id",
{ templateUrl: "/client/views/details.html" })
.otherwise(
{ redirectTo: "/list" });
//$locationProvider.html5Mode(true);
//$locationProvider.html5Mode(true).hashPrefix('!')
}
app.config(["$routeProvider", "$locationProvider",config]);
My anchor tags href are like this:
Details
Details
Details
You can href without putting #
Details
Details
Details
A Plunker example here
Plunker

Is there a "null" controller for routes that do not need a controller in AngularJS? [duplicate]

Is it possible to use Angularjs's routing and controller without the templateURL?
For instance, below is my current routes, controllers, and template urls,
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
templateUrl: "js/template/1.html",
controller: "controller1"
})
.when("/list1",
{
templateUrl: "js/template/2.html",
controller: "controller2"
})
...
And I have ng-view in my html,
<div ng-view></div>
I tested with the routes without templateUrl,
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
//templateUrl: "js/template/1.html",
controller: "controller1"
})
.when("/view1",
{
//templateUrl: "js/template/2.html",
controller: "controller2"
})
...
the result - nothing is displayed on my screen. obviously the controller cannot be triggered anymore.
use template with empty string.
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
template: "",
controller: "controller1"
})
Angular uses the $routeProvider Definition to attach a controller to your view. If you don't want to specify the details in $routeProvider config, other option is to let the application land on one page using something like -
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html'
})
.when()
.otherwise({
redirectTo: '/'
});
});
This way user will land on main.html. Now on the main.html you can include several different html templates like -
<div ng-include src="'js/template/1.html'" ng-controller = 'Controller1'></div>
<div ng-include src="'js/template/2.html'" ng-controller = 'Controller2'></div>
Notice the syntax of using/including template above - "'<<Path>>'"
Hope this helps.
Setting template with empty string make error and cause recursion loading ; i solved it by just add the link of empty html file.
return app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/",
{
template: "dummy.htm",
controller: "controller1"
})

Page refresh gives 404

I am using angular router for routing my app and I want to use the html5mode.
Routing is working fine, but when a refresh my page (on he browser), I get a 404 error.
Here is my routing code snipet
angular.module ('app', ['ngRoute', 'home', 'login', 'admin'])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/components/home/home.html',
controller: 'homeController'
})
.when('/login', {
templateUrl: 'app/components/login/login.html',
controller: 'loginController'
})
.when('/outils-de-gestion', {
templateUrl: 'app/components/admin/admin.html',
controller: 'adminController'
})
$locationProvider.html5Mode(true);
}])
.controller ('appController', ['$scope', function ($scope){
}])
What am I doing wrong ? Has anyone an idea about that ?
This solution works for angularJs.
index.html => Add in head tag.if your application is in folder then you need to add folders like:
config.js => add $locationProvider in function param and the code below:
app.config(function($routeProvider,$locationProvider)
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
and your href should like ng-href="home" i.e. do not use #or !
thats it .

AngularJS $locationProvider.html5Mode not working on localhost

I'm trying to get rid of the hashtag that appears in my ui-router URLs. (http://localhost:3000/email doesn't work but http://localhost:3000/#email works. After scouring SO, I haven't found a case that works for me. I'm running locally for now, so I assume I don't need any "Server configuration" and I included "" in my index.html.
(function(angular) {
'use strict';
angular.module('myApp', ['ui.router'])
.controller('MainController', function($scope, $route, $routeParams, $location) {
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
})
.config([
['$stateProvider', '$locationProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider, $locationProvider, $provide) {
$urlRouterProvider.otherwise('/email');
// PAGES
$stateProvider
.state('email', {
url: '/email',
templateUrl: '../pages/email.html',
controller: 'EmailController'
})
.state('about', {
url: '/about',
templateUrl: '../pages/about.html',
controller: 'AboutController'
})
// ... the rest...
$locationProvider
.html5Mode(true); // enable html5Mode for pushstate ('#'-less URLs DOESN'T WORK)
.hashPrefix('!');
$provide.decorator('$sniffer', function($delegate) {
$delegate.history = false;
return $delegate;
});
}]);
You can either do this in your run block:
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
And your url's will be changed from domain.com/#foo to domain.com/foo (this requires no explicit base).
...or do this in your run block:
$locationProvider.html5Mode(true);
...and then add this to your html <head>:
<base href="/">
This solution provides the same outcome as the first, except that now there is a specified base.
Perhaps try this code? I think it may be down to one of these reasons.
Config dependencies weren't set correctly (out of order)
That .hasPrefix(!) was put in after a semi-colon.
Also need to assign a base location in your <head> tag using <base href='/'> (or maybe '/email' ?)
angular.module('myApp', ['ui.router'])
.controller('MainController', function ($scope, $route, $routeParams, $location) {
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
})
.config(function ($stateProvider, $urlRouterProvider, $locationProvider, $provide) {
$urlRouterProvider.otherwise('/email');
// PAGES
$stateProvider
.state('email', {
url: '/email',
templateUrl: '../pages/email.html',
controller: 'EmailController'
})
.state('about', {
url: '/about',
templateUrl: '../pages/about.html',
controller: 'AboutController'
});
$locationProvider.html5Mode(true).hashPrefix('!');
$provide.decorator('$sniffer', function ($delegate) {
$delegate.history = false;
return $delegate;
});
});
Apologies, I can't get the code sample to behave.
Did you set base in HTML-file same as follow:
<html>
<head>
<base href="/">
</head>
</html>
and you should remove .hashPrefix('!'); at config, so this code will same here:
$stateProvider
.state('email', {
url: '/email',
templateUrl: '../pages/email.html',
controller: 'EmailController'
})
.state('about', {
url: '/about',
templateUrl: '../pages/about.html',
controller: 'AboutController'
})
// ... the rest...
$locationProvider
.html5Mode(true); // enable html5Mode for pushstate ('#'-less URLs DOESN'T WORK)
$provide.decorator('$sniffer', function($delegate) {
$delegate.history = false;
return $delegate;
});
Your app config will look like this
app.config(function ($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {
templateUrl: 'url',
controller : 'mainController'
})
});
Use tag in your page head
<base href="abc.com/"/>
This may create problem with following href, will not load the page properly
About Us
To overcome this use target="_self" in anchor tag as follow
About Us

How to pass and read multiple params in URL - angularjs

There are 2 views with respective controllers.
The links are in View1.Clicking on this link should load View2 and also read the parameters.
This is what I have tried but the alert says - undefined.
View2 can load with/without params - each case having a different workflow.
View1.html:
<div ng-controller="view1ctrl">
<a href="#/view2/pqid/775/cid/4" >Link1</a>
</div>
app.js:
var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/view1', {
templateUrl: 'App/views/view1.html',
controller: 'view1ctrl'
})
.when('/view2', {
templateUrl: 'App/views/view2.html',
controller: 'view2ctrl'
})
.when('/view2/:pqid/:cid', {
templateUrl: 'App/views/view2.html',
controller: 'view2ctrl'
})
.otherwise({
redirectTo: '/view1'
});
}]);
view2ctrl.js:
app.controller("view2ctrl", ['$scope','$routeParams',function ($scope, $routeParams) {
var init = function () {
alert($routeParams.pqid);
}
init();
}]);
You are nearly there:
.when('/view2/:pqid/:cid'
maps to a URL in this format :
view2/1234/4567
1234 being the pqid and 4567 the cid.
So your routing, I think is working, just change the link to #/view2/775/4.
Or if you want to keep the same link change your routing to:
#/view2/pqid/:pqid/cid/:cid

Resources