i am learning how to use angularjs routing and templating, and i have come across a problem. I have added the gem in Gemfile, added //= require angular-rails-templates in application.js, and added the dependancy in my main angular.module
angular.module('authentication', ['authenticationModule','ngRoute','templates'])
.config(['$routeProvider',function($provider){
$provider
.when('/sign_in', { templateUrl: '/assets/signin.html', controller: 'redirectCtrl'})
.when('/sign_up', { templateUrl: 'templates/signup.html.erb', controller: 'redirectCtrl1'})
.controller('redirectCtrl', function($location) {
$location.path('/sign_in')
})
.controller('redirectCtrl1', function($location) {
$location.path('/sign_up')
})
My signin.html is located in /assets/templates/signin.html
When i try to load the page, i dont get the rendered page, but instead i get this
window.AngularRailsTemplates || (window.AngularRailsTemplates = angular.module("templates", [])); window.AngularRailsTemplates.run(["$templateCache",function($templateCache) { $templateCache.put("signin.html", "\u003cdiv ng-controller=\"authenticationCtrl\"\u003e\n\t\u003cdiv\u003e\n\t \u003ch1\u003eLogin\u003c/h1\u003e\n\t\u003c/div\u003e\n\t\u003cdiv\u003e\n\t\t\u003cdiv\u003e\n\t\t \u003cdiv\u003eEmail: \u003cinput ng-model=\"email\" type=\"email\" /\u003e\u003c/div\u003e\n\t\t \u003cdiv\u003ePassword: \u003cinput type=\"password\" ng-model=\"password\" /\u003e\u003c/div\u003e\n\n\t\t \u003cbutton ng-click=\"login()\" \u003eLogin\u003c/button\u003e\n\t\t\u003c/div\u003e\n\t\u003c/div\u003e\t\t\t\t\n\u003c/div\u003e\n"); }]);
Any solution to my problem?
Thanks in advance
Update 1:
Ok,so i have changed the following things,
angular.module('authentication', ['authenticationModule','ngRoute','templates'])
.config(['$routeProvider',function($provider){
$provider
.when('/sign_in', { templateUrl: 'assets/signin.html', controller: 'redirectCtrl'})
.when('/sign_up', { templateUrl: 'assets/signup.html', controller: 'redirectCtrl1'})
}])
.controller('redirectCtrl', function($location) {
// $location.path('/sign_in')
})
.controller('redirectCtrl1', function($location) {
// $location.path('/sign_up')
})
And here is what i get now, the template loads but with some extra stuff that i dont need
http://postimg.org/image/iuhy9dflj/
remove the / before assets, and point to the templates folder
angular.module('authentication', ['authenticationModule','ngRoute','templates'])
.config(['$routeProvider',function($provider){
$provider
.when('/sign_in', { templateUrl: 'assets/templates/signin.html', controller: 'redirectCtrl'})
.when('/sign_up', { templateUrl: 'templates/signup.html.erb', controller: 'redirectCtrl1'})
.controller('redirectCtrl', function($location) {
$location.path('/sign_in')
})
.controller('redirectCtrl1', function($location) {
$location.path('/sign_up')
})
Related
I want to add fusioncharts to my angularjs app. I have followed this link:
http://www.fusioncharts.com/blog/2015/03/angular-fusioncharts/
But as soon as I add ng-fusioncharts to my app.js as:
var app = angular.module('myApp',['ngRoute', 'firebase','ng-fusioncharts']);
app.config(function($routeProvider){
$routeProvider
.when('/', {
controller: 'OrgListController',
templateUrl: 'views/organization/list.html'
})
.when('/add_org', {
controller: 'OrgAddController',
templateUrl: 'views/organization/org_add.html'
})
.when('/edit_org/:id', {
controller: 'OrgEditController',
templateUrl: 'views/organization/org_edit.html'
})
.when('/add_access_point', {
controller: 'AccessPointAdd',
templateUrl: 'views/access_points/access_add.html'
})
.when('/edit_access_point/:id', {
controller: 'AccessPointEdit',
templateUrl: 'views/access_points/access_edit.html'
})
.when('/add_user', {
controller: 'UserAdd',
templateUrl: 'views/users/user_add.html'
})
.when('/edit_user/:id', {
controller: 'UserEdit',
templateUrl: 'views/users/user_edit.html'
})
.otherwise({
redirectTo: '/'
});
});
Nothing gets displayed and my whole project doesn't work. Viewing in console says :
error: inject modulerr.................
Somebody please suggest how to add multiple dependencies in one module?
You need to have fusion-chart.js inorder to inject ng-fusioncharts
var app = angular.module('HelloApp', ["ng-fusioncharts"])
DEMO
My team and I are developing a muli-page MVC application using AngularJS and DotNetNuke. In our routes.js, we point to specific controllers and templates.
Here is our routes.js code:
angular.module('SurveyWrangler').config(function ($routeProvider) {
$routeProvider
.when('', {
redirectTo: '/surveys'
})
.when('/', {
redirectTo: '/surveys'
})
.when('#', {
redirectTo: '/surveys'
})
.when('/surveys', {
controller: "SurveyIndexController",
templateUrl: "/DesktopModules/HealthPlanSurvey/templates/survey/index.html"
})
.when('/surveys/new', {
templateUrl: "/DesktopModules/HealthPlanSurvey/templates/survey/create.html",
controller: "SurveyCreateController"
})
.when('/surveys/:id', {
templateUrl: "/DesktopModules/HealthPlanSurvey/templates/survey/show.html",
controller: "SurveyShowController"
})
.when('/surveys/:id/edit', {
templateUrl: "/DesktopModules/HealthPlanSurvey/templates/survey/edit.html",
controller: "SurveyEditController"
})
.when('/surveys/:id/carryforward', {
templateUrl: "/DesktopModules/HealthPlanSurvey/templates/survey/carryForward.html",
controller: "SurveyCarryForwardController"
})
});
The link for the app is www.OURWEBSITE.com/Surveys#/surveys, but the URL changes to just ~/Surveys# when you click the link in the menu bar.
The menu bar is rendered out by DNN. This is baffling to me because from what I can tell from the code, I should be redirected back to ~/Survey#/surveys. Instead, I get a blank screen. Also, no errors in the browser console.
Thanks for any help!
I'm a newbie with AngularJS and I got a problem that I think that's it's can be configurable in my routeProvider.
I have this route
angular
.module('app', ['ngRoute', 'ngStorage'])
.config(['$routeProvider', function ($routeProvider) {
debugger;
$routeProvider.when('/:module/:task/:id/:menu/:action', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task/:id/:menu', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task/:id', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/', { templateUrl: 'app/start.html' });
$routeProvider.otherwise({ redirectTo: '/' });
}
]);
the problem: When I just type http://localhost:53379 I'm redirected to http://localhost:53379/#/ . Where come from the /#/ ?
By default, AngularJS will route URLs with a hashtag.
For example:
http://domain.com/#/home
http://domain.com/#/about
You can very easy remove the hashtag from the URL by setting html5Mode to true in your config:
$locationProvider.html5Mode(true);
so in your code it will be:
angular
.module('app', ['ngRoute', 'ngStorage'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
debugger;
$routeProvider.when('/:module/:task/:id/:menu/:action', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task/:id/:menu', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task/:id', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module/:task', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/:module', { templateUrl: 'app/blank.html', controller: PagesCtrl });
$routeProvider.when('/', { templateUrl: 'app/start.html' });
$routeProvider.otherwise({ redirectTo: '/' });
$locationProvider.html5Mode(true);
}
]);
Just after that you have to make sure that your backed will redirect all requests to your home page if you are doing "Single Page App"
Angular adds it by default. I don't know it this is the main reason, but one reason is that the routing doesn't work in older versions of IE. I had this problem in one angularjs app that didn't work in IE9 because of this reason.
Anyways, to remove the hashtag simply add $locationProvider.html5Mode(true); after your routing-declarations.
You can read more about it here: http://scotch.io/quick-tips/js/angular/pretty-urls-in-angularjs-removing-the-hashtag
This /#/ is used to create a single page application. the # is used to prevent that the page is completely reloaded. Angular then catches the new URL and loads the correct controller and partials depending on your route configuration.
Since HTML5 it is possible to remove this behavior with $location.html5Mode(true).
Source:
AngularJS documentation
I have the following bit of code for my navigation that I want to update dynamically between pages.
<nav ng-include="menuPath"></nav>
Here is my app and routing set up
var rxApp = angular.module('ehrxApp', ['ngRoute']);
// configure our routes
rxApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'mainController',
templateUrl: '/content/views/index.html'
})
.when('/census', {
templateUrl: '/content/views/admission/census.html',
controller: 'censusController'
})
.when('/messages', {
templateUrl: '/content/views/account/messages.html',
controller: 'messagesController'
})
.when('/profile', {
templateUrl: '/content/views/account/profile.html',
controller: 'profileController'
})
});
In my main controller I set the menuPath value here:
rxApp.controller('mainController', function (userService, $scope, $http) {
evaluate_size();
$scope.menuPath = "/content/views/index.menu.html";
});
rxApp.controller('censusController', function ($scope, $http, $sce, censusService) {
$scope.menuPath = "/content/views/admission/census.menu.html";
evaluate_size();
});
When the page switches to the census view it should change the menu. What happens though is the first page loads the main menu, then no matter what other page you go to the menu never updates.
I imagine this problem has something to do with a primitive values and prototypical inheritance between child scopes, but would need to see more of your html to determine that. Without that, I propose an alternative way that may solve your problem and keep the config all in one place.
$routeProvider will accept variables and keep them on the route, even if angular doesn't use them. so we modify your routing by including the menuPath like so:
var rxApp = angular.module('ehrxApp', ['ngRoute']);
// configure our routes
rxApp.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'mainController',
templateUrl: '/content/views/index.html',
menuPath: '/content/views/index.menu.html'
})
.when('/census', {
templateUrl: '/content/views/admission/census.html',
controller: 'censusController',
menuPath: '/content/views/admission/census.menu.html'
})
.when('/messages', {
templateUrl: '/content/views/account/messages.html',
controller: 'messagesController'
})
.when('/profile', {
templateUrl: '/content/views/account/profile.html',
controller: 'profileController'
})
});
Remove setting $scope.menuPath from each controller, then finally add a watch on rootScope that will change the menuPath on $routeChangeSuccess
rxApp.run(['$rootScope', function ($rootScope) {
$rootScope.$on('$routeChangeSuccess', function(event, current) {
if (current && current.$$route && current.$$route.menuPath) {
$rootScope.menuPath = current.$$route.menuPath;
} else {
$rootScope.menuPath = '';
}
});
}]);
Right now i am using routeProvider to change between views which works awesome. But now i want to create a view which contains 4 different tabs which should contain 4 different controllers. ive read here that it could be done with stateProvider:
Angular ui tab with seperate controllers for each tab
here is my code:
var WorkerApp = angular.module("WorkerApp", ["ngRoute", 'ngCookies', "ui.bootstrap", "ngGrid", 'ngAnimate', 'ui.router']).config(function ($routeProvider, $stateProvider) {
$routeProvider.
when('/login', {
templateUrl: 'Home/Template/login', resolve: LoginCtrl.resolve
})
.when('/register', { templateUrl: 'Home/Template/register', resolve: RegisterCtrl.resolve })
.when('/', { templateUrl: 'Home/Template/main', resolve: MainCtrl.resolve })
.when('/profile', { templateUrl: 'Home/Template/profile', controller: "ProfileController" })
.when('/contact', { templateUrl: 'Home/Template/contact', controller: "ContactController" })
$stateProvider.state('tabs', {
abstract: true,
url: '/profile',
views: {
"tabs": {
controller: "ProfileController",
templateUrl: 'Home/Template/profile'
}
}
}).state('tabs.tab1', {
url: '/profile', //make this the default tab
views: {
"tabContent": {
controller: "ProfileController",
templateUrl: 'Home/Template/profile'
}
}
})
.state('tabs.tab2', {
url: '/tab2',
views: {
"tabContent": {
controller: 'Tab2Ctrl',
templateUrl: 'tab2.html'
}
}
});
});
but i cant get it really to work because default of routeprovider is set to send over to work because my routeprovider is sending over to "/" on default, which makes "/tabs" invalid. so i cant actully figure out if it is possible to switch to states on specific url. Or change state on specific URL in routeProvider?
I can't tell you for sure exactly what's wrong with the code you've provided, but I'm using Angular UI-Router with the same use case you described, and it's working for me. Here's how I have it configured and how it's different from your configuration:
I don't use $routeProvider at all (none of your $routeProvider.when statements). I'm pretty sure you should not be using $routeProvider since you're using $stateProvider.
I have one use of the $urlRouterProvider with an 'otherwise' statement to specify a default URL:
$urlRouterProvider.otherwise("/home");
My calls to $stateProvider.state is a little different from yours. Here's the one for the parent view of the tabs:
$stateProvider.state('configure', {
url: "/configure",
templateUrl: 'app/configure/configure.tpl.html',
controller: 'ConfigureCtrl'
});
Here's an example of the child state (really the same except for the state name being parent.child format, which you already have in your code; and I added a resolve block but you could have that on the parent as well):
$stateProvider.state('configure.student', {
url: "/student",
templateUrl: 'app/configure/student/configure.student.tpl.html',
controller: 'ConfigureStudentCtrl',
resolve: {
storedClassCode: function($q, user, configureService) {
return configureService.loadMyPromise($q, user);
}
}
});
Also, I'm using version 0.2.8 of Angular UI-Router with version 1.2.9 of Angular. I think this would work with any version of Angular 1.2.0 or later.