I am currently building a Angularjs singel page application and i have run in to some odd error with my routing.
I have a nav bar with multiple options and all but one of them work correct.
This is my route provider.
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when("/index", {
templateUrl: "/App/Views/index.html",
controller: "IndexController"
})
.when("/log", {
templateUrl: "/App/Views/log.html",
controller: "LogController"
})
.when("/help", {
templateUrl: "/App/Views/help.html",
controller: "HelpController"
})
.when("/login", {
templateUrl: "/App/Views/login.html",
controller: "LoginController"
})
.when("/signup", {
templateUrl: "/App/Views/signup.html",
controller: "SignupController"
})
.when("/logout", {
templateUrl: "/App/Views/logout.html",
controller: "LogOutController"
})
.when("/user", {
templateUrl: "/App/Views/user.html",
controller: "UserController"
})
.otherwise({
redirectTo: "/index"
});
}]);
And this is my Navbar
<li>
Hem
</li>
<li ng-class="{ activeLink: isActive('/log')}" data-ng-hide="!isLoggedIn">
Logg
</li>
<li ng-class="{ activeLink: isActive('/help')}" data-ng-hide="!isLoggedIn">
Hjälp
</li>
<li ng-class="{ activeLink: isActive('/logout')}" data-ng-hide="!isLoggedIn">
Logga ut
</li>
<li ng-class="{ activeLink: isActive('/user')}" data-ng-hide="!isLoggedIn">
{{userName}}
</li>
And my problem is that when i click the {{userName}} link i am taken to the index page.
And this error only occurs on the live server when the js is minified, if it is unminified on the server so does it work like intended.
If i run this site localy i can use both the regular version and the minified one and the routing behaves like intended.
The console does not show any errors when i click on the link and whn i inspect the html in chrome so does it have the correct html
Johan Karlsson.
I have tried pointing the {{userName}} link to "#/log" and it worked fine.
Here is my user controller
(function () {
var app = angular.module("connectPortal");
var userController = ['$scope', 'authService', function ($scope, authService) {
authService.authenticateUser();
$scope.var = "var variable from HelpController";
}];
app.controller("UserController", userController);
}());
And this is my log controller
(function () {
var app = angular.module("connectPortal");
var logController = ['$scope', 'authService', function ($scope, authService) {
authService.authenticateUser();
$scope.var = "var variable from LogController";
}];
app.controller("LogController", logController);
}());
Related
Maybe someone will help me. I write an app in angularjs, I have a file named list.html which retrieves a list of posts from jsonplaceholder and lists them, with a link to the details of the post. In $ routeParams, I pass the id of the selected one and pick it up. Unfortunately, I have no idea how to download the details of a post and display them in the details.html file. If I want to remove something for example, I write for example $ scope.deletePost as a function and give an id, but how to list details I have no idea.
//routing.js
var myApp = angular.module('myApp', ["ngRoute"])
myApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/test', {
templateUrl: '/event/example.html',
controller: 'exampleController'
}, null)
.when('/list', {
templateUrl: '/event/list.html',
controller: 'exampleController'
}, null)
.when('/test-list', {
templateUrl: '/test/list.html',
controller: 'testController'
}, null)
.when('/test/:id', {
templateUrl: '/test/details.html',
controller: 'testController'
}, null)
}
]);
//controller.js
angular.module('myApp').controller('testController', function ($scope, $http, $routeParams) {
$http.get('https://jsonplaceholder.typicode.com/posts').then(function (response) {
$scope.posts = response.data;
});
$scope.id = $routeParams.id;
});
//details.html
<div data-ng-controller="testController">
{{data}}
</div>
//list.html
<div data-ng-controller="testController">
<ul>
<li ng-repeat="post in posts">
Tytuł: {{post.title}} <a href="#!test/{{post.id}}" >Show</a>
</li>
</ul>
</div>
Check out this plunkr.
You just need to pass the details using ng-href and then catch in the controller using $routeParams. I hope this would help you with what you were looking for.
var app = angular.module( 'mainApp', ['ngRoute'] );
app.config( function( $routeProvider ) {
$routeProvider
.when( '/main', {
templateUrl: 'list.html',
controller: 'listCtrl'
})
.when('/detail/:id', {
templateUrl: 'detail.html',
controller: 'detailCtrl'
})
.otherwise({
redirectTo: '/main'
});
});
app.controller( 'listCtrl', function( $scope, $http) {
$http.get('https://jsonplaceholder.typicode.com/posts')
.then(function(res){
$scope.data = res.data;
})
});
app.controller( 'detailCtrl', function( $scope,$http, $routeParams) {
$scope.id = $routeParams.id;
$http.get('https://jsonplaceholder.typicode.com/posts/'+$scope.id)
.then(function(res){
$scope.data = res.data;
})
});
So I am using ui-view to route me to a partial.
//route.provider.js
(function() {
'use strict';
angular
.module('app')
.provider('RouteService', RouteService);
RouteService.$inject = ['$stateProvider', '$urlRouterProvider'];
function RouteService ($stateProvider, $urlRouterProvider) {
var service = {};
this.$get = function() { return service; };
this.initialize = function() {
$urlRouterProvider.otherwise('/login');
$stateProvider
.state('home', {
url: '/home',
controller: 'HomeController',
templateUrl: 'home/home.view.html',
controllerAs: 'viewModel'
})
.state('login', {
url: '/login',
controller: 'LoginController',
templateUrl: 'login/login.view.html',
controllerAs: 'viewModel'
})
//TODO Impliment the following partial pages...
.state('gensim', {
url: '/gensim',
templateUrl: 'general-simulation/simulation.view.html',
controller: 'TabsController',
controllerAs: 'tabs'
})
<...more routes...>
}
}
})();
The issue I am having is once it routes to
// general-simulation/simulation.view.html
I'd like it to use a custom directive to insert more html into the page.
Inside simulation.view.html I have this.
<div class="container">
<div class="row center">
<div class="col-xs-12">
<h1>General Simulation</h1>
<gs-tabs><h1>TEST!!!!</h1></gs-tabs>
</div>
</div>
</div>
The directive is constructed in
// tabs.directives.js
(function(){
'use strict';
angular
.module('app')
.directive('gsTabs', gsTabs);
function gsTabs () {
return {
restrict: "E",
templateURL: 'tabs.view.html'
};
}
})();
Finally, my tabs.view.html looks like this.
<h1>YOU HAVE ENTERED TABS PARTIAL</h1>
When I navigate to the page that displays simulation.view all I can see is:
General Simulation
TEST!!!!
So what am I doing wrong here. I checked for camelCasing and the page is not showing in errors. Any help would be greatly appreciated!
Update:
I viewed the network calls in chrome's dev tools.
simulation.view.html is being loaded but tabs.view.html isn't.
Request URL:http://sdc-stagt01/AngularJS/general-simulation/simulation.view.html
Request Method:GET
Status Code:200 OK
Remote Address:10.19.8.96:80
The file substructure is:
/general-simulation/tabs/tabs.controller.js
/general-simulation/tabs/tabs.directives.js
/general-simulation/tabs/tabs.view.html
/general-simulation/simulation.view.html
putting comment as answer
change templateURL to templateUrl
So entre's comment was a big help. That got chrome dev to start spitting out errors.
charlietfl Was on the right track with mentioning my file structure as well. I needed to change my directive to look like this:
templateUrl: 'general-simulation/tabs/tabs.view.html'
Thank you!
You have a typo here. templateUrl not templateURL
function gsTabs () {
return {
restrict: "E",
templateURL: 'tabs.view.html' // change this to templateUrl
};
}
Fairly new to angularjs, trying to make a single page application.
The default page to load should be a home page, and I also want a home button to route to this page.
Nav bar in HTML:
<html ng-app="app">
...
<ul class="nav navbar-nav navbar-center">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
...
<div ng-view></div>
in app.module.js:
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'pages/home.html',
controller: 'main',
controllerAs: 'vm'
})
.when('/about', {
templateUrl: 'pages/about.html',
controller: 'about',
controllerAs: 'vm'
})
.when('/contact', {
templateUrl: 'pages/contact.html',
controller: 'contact',
controllerAs: 'vm'
});
});
app.controller('main', main);
app.controller('about', about);
app.controller('contact', contact);
function main() {
var vm = this;
vm.message = "Home page";
}
function about() { //stuff }
function contact() { //stuff }
When the page loads, everything displays correctly, and the url is http://localhost:8007/#/
My problem is that when I click Home, the url becomes http://localhost:8007/# and nothing displays. About and Contact both work as intended.
I know I can just change my Home button to have href="#/" but that feels like I am targeting the symptoms, not the problem. I feel like I really shouldn't have to do that.
I have tried adding .when('', { to app.module.js, but this does nothing.
Your home href will be Home which then translates to the path "/" you defined in $routeProvider.
Dont use href="#" anywhere in the app
I'm new to AngularJS and stuck on below code.
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "partials/home.html",
controller: "mainController",
})
.when('/products', {
templateUrl: "partials/productlist.html",
//controller: "ProductController",
})
.when('/product/:prodID', {
templateUrl: "partials/product.html",
controller: "viewController",
})
.when('/contact', {
templateUrl: "partials/contact.html",
controller: "contactController",
})
.otherwise({
redirectTo: "/"
});
});
app.controller('ProductController', function($scope, $http){
$http.get('partials/productTable.json').success(function(response){
$scope.datap = response.lists;
});
}).
controller('viewController',function($scope,$routeParams){
$scope.eachproduct = $scope.datap[$routeParams.prodID];
});
And my product.html page code will look like this.
<div ng-controller="viewController">
<ol class="breadcrumb">
<li>Home</li>
<li>Products</li>
<li class="active">{{eachproduct.link}}</li>
</ol>
<div class="col-md-4">
<figure><img ng-src="{{ }}"></figure>
<p>
Read More
</p>
</div>
</div>
Problem is when I navigate to any product page value of {{eachproduct.link}} is not showing.
Any solution will be appriciated.
Use $rootScope instead of $scope
$rootScope
The $rootScope is the top-most scope. An app can have only one $rootScope which will be shared among all the components of an app. Hence it acts like a global variable. All other $scopes are children of the $rootScope.
Sample :
controller('viewController',['$scope','$routeParams', '$http','$rootScope',function($scope,$routeParams, $http,$rootScope){
$http.get('partials/productTable.json').success(function(response){
$scope.datap = response.lists;
$rootScope.eachproduct = $scope.datap[$routeParams.prodID];
});
}]);
app.controller('ProductController', function($scope, $http){
$http.get('partials/productTable.json').success(function(response){
$scope.datap = response.lists;
});
}).
controller('viewController',function($scope,$routeParams, $http){
$http.get('partials/productTable.json').success(function(response){
$scope.datap = response.lists;
$scope.eachproduct = $scope.datap[$routeParams.prodID];
});
});
It seems like what you are looking for is an angular provider such as a factory to store the values in, this will allow the values to be pass values around the controllers while using the routes.
Have a look at this example, while it isn't using routes, the principal is the same:
https://jsbin.com/wiwejapiku/edit?html,js,output
For more information on providers have a look here:
https://docs.angularjs.org/guide/providers
Your example would work something like this:
app
.factory('productFactory',function(){
return {
data: {}
};
})
.controller('ProductController', function($scope, $http, productFactory){
$scope.productFactory = productFactory;
$http.get('partials/productTable.json').success(function(response){
$scope.productFactory.data = response.lists;
});
}).
controller('viewController',function($scope,$routeParams, productFactory){
$scope.productFactory = productFactory;
$scope.eachproduct = $scope.productFactory.data[$routeParams.prodID];
});
Note you would also have to change your view to reference 'productFactory.data' respectively.
I'm struggling to make my category and vendor name visible on breadcrumbs.
I'm using ng-breadcrumbs module (https://github.com/ianwalter/ng-breadcrumbs).
I guess I have problem with making curCategory and curVendor globally acessible, but tried some ways and can't make it working.
Here is my html:
<body ng-controller="mainController as main">
(...)
<ol class="breadcrumb">
<li ng-repeat="breadcrumb in breadcrumbs.get({'Category':curCategory,'Vendor': curVendor}) track by breadcrumb.path" ng-class="{ active: $last }">
<a ng-if="!$last" ng-href="#{{ breadcrumb.path }}" ng-bind="breadcrumb.label" class="margin-right-xs"></a>
<span ng-if="$last" ng-bind="breadcrumb.label"></span>
</li>
</ol>
My routes:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'templates/pages/main.html',
label: 'Home'
}).
when('/:cat', {
templateUrl: 'templates/pages/category.html',
label: 'Category'
}).
when('/:cat/:ven', {
templateUrl: 'templates/pages/vendor.html',
label: 'Vendor'
}).
otherwise({
redirectTo: '/'
});
}]);
My controller:
var app = angular.module("myApp", [
'ngRoute',
'ng-breadcrumbs'
]);
app.controller('mainController', function($scope, $routeParams, breadcrumbs) {
$scope.breadcrumbs = breadcrumbs;
$scope.curCategory = $routeParams.cat;
$scope.curVendor = $routeParams.ven;
(...)
});
Uff I found what was wrong!
What suprised me was that inside specific controllers it worked fine, but when I moved it to mainController it returned empty object from $routeParams.
Here is solution:
$scope.$on('$routeChangeSuccess', function() {
$scope.curCategory = $routeParams.cat;
$scope.curVendor = $routeParams.ven;
});
It just assigned variables before $routeParams were populated.