Clean URLs for Angularjs - angularjs

I'm trying to use clean url routing for my web app but everything I try doesnt work. Here is my app.js. Im not to sure how the location provider should be added in to the app.js but everything I try produces a 404. Please could someone point me in the right direction?
.config(function($stateProvider, $urlRouterProvider) {
//Enable cross domain calls
$stateProvider
.state('main', {
url: '/',
templateUrl: 'views/main.html',
controller: 'MainCtrl',
controllerAs: 'vm'
})
.state('about', {
url: '/about',
templateUrl: 'views/about.html',
controller: 'AboutCtrl',
controllerAs: 'vm'
})
.state('blog', {
url: '/blog',
templateUrl: 'views/blog.html',
controller: 'BlogCtrl',
controllerAs: 'vm'
})
.state('corparate', {
url: '/corparate',
templateUrl: 'views/corparate.html',
controller: 'CorparateCtrl',
controllerAs: 'vm'
})
.state('contact', {
url: '/contact',
templateUrl: 'views/contact.html',
controller: 'ContactCtrl',
controllerAs: 'vm'
})
.state('login', {
url: '/login',
templateUrl: 'views/login.html',
controller: 'RegisterationCtrl',
controllerAs: 'vm'
})
.state('register', {
url: '/register',
templateUrl: 'views/register.html',
controller: 'RegisterationCtrl',
controllerAs: 'vm'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'views/dashboard.html',
controller: 'DashboardCtrl',
controllerAs: 'vm'
})
.state('useredit', {
url: '/settings',
templateUrl: 'views/useredit.html',
controller: 'UserEditCtrl',
controllerAs: 'vm'
})
.state('friendlist', {
url: '/search',
templateUrl: 'views/friendlist.html',
controller: 'FriendListCtrl',
controllerAs: 'vm'
})
.state('friendprofile', {
url: '/profile/:userName',
templateUrl: 'views/friendprofile.html',
controller: 'FriendProfileCtrl',
controllerAs: 'vm'
})
.state('resetPassword', {
url: '/resetPassword',
templateUrl: 'views/resetPassword.html',
controller: 'ResetPasswordCtrl',
controllerAs: 'vm'
});
$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get("$state");
$state.go("main");
});
})
.run(function ($rootScope, $state, ParseApi) {
$rootScope.$on('$stateChangeStart', function (event, next) {
if (!ParseApi.isAuthenticated()) {
if (next.name !== 'register' && next.name !== 'login' && next.name !== 'resetPassword' && next.name !== 'friendlist' && next.name !== 'friendprofile') {
event.preventDefault();
$state.go('login');
}
}
});
})
})();

Related

angularjs ng-route. not work templateUrl

It does not work with the specified templateUrl: test.html but works if you specify the template: 'Hello '
WORK
app.config(function($routeProvider){
$routeProvider
.when('/test', {
template: '<h1>home</h1>',
controller: 'ctrl'
})
// .otherwise({
// redirectTo: '/home',
// controller: 'ProductCtrl'
// });
});
DO NOT WORK
app.config(function($routeProvider){
$routeProvider
.when('/test', {
templateUrl: 'test.html',
controller: 'ctrl'
})
// .otherwise({
// redirectTo: '/home',
// controller: 'ProductCtrl'
// });
});
and I have a sign ! in the path to the file that is #!/test
LINK
<a style="color:white;" href="#!/test">Go</a>
I'm using nw.js
You could try to put a path to your template url
.when('/test', {
templateUrl: './test.html',
controller: 'ctrl'
})

Could not resolve 'state1' from state 'state2'

Im transitioning from one state to another using $state.go() like below:
$state.go('menuItem.list');
This causes following error.
Could not resolve 'menuItem.list' from state 'branches.view'
Below are the state providers for menuItem and branch modules.
module : branch
.state('branches', {
abstract: true,
url: '/branches',
template: '<ui-view/>'
})
.state('branches.list', {
url: '',
templateUrl: 'modules/branches/client/views/list-branches.client.view.html',
controller: 'branchesListController',
controllerAs: 'vm'
})
.state('branches.view', {
url: '/:branchId',
templateUrl: 'modules/branches/client/views/view-branch.client.view.html',
controller: 'branchesController',
controllerAs: 'vm'
})
module : menuItem
.state('menuItems', {
abstract: true,
url: '/menuItems',
template: '<ui-view/>'
})
.state('menuItems.list', {
url: '',
templateUrl: 'modules/menuItems/client/views/list-menuItems.client.view.html',
controller: 'menuItemsListController',
controllerAs: 'vm'
})
This way you can all modules in new module file
angular.module('branches.modules', [
'branches', 'branches.list', 'branches.view', 'menuItems','menuItems.list'
]);
and add that in your main app.js or app.module.js
angular.module('app', ['ui.router', 'branches.modules']);
yo can use code below :
module : branch
.state('branches', {
abstract: true,
url: '/branches',
template: '<ui-view/>'
})
.state('branches.list', {
url: '',
views: {
templateUrl: 'modules/branches/client/views/list-branches.client.view.html',
controller: 'branchesListController',
controllerAs: 'vm'
}
})
.state('branches.view', {
url: '/:branchId',
views: {
templateUrl: 'modules/branches/client/views/view-branch.client.view.html',
controller: 'branchesController',
controllerAs: 'vm'
}
})
module : menuItem
.state('menuItems', {
abstract: true,
url: '/menuItems',
template: '<ui-view/>'
})
.state('menuItems.list', {
url: '',
views:{
templateUrl: 'modules/menuItems/client/views/list-menuItems.client.view.html',
controller: 'menuItemsListController',
controllerAs: 'vm'
}
})
you must use views in state
Good Luck

AngularJS caching of ng-view templates (under MVC project) for offline?

Here is my new post: AngularJS $templateCache under MVC
I have this routing set up in my MVC-Angular-TypeScript project:
export let app = angular.module("app", ["ngRoute", "ngSanitize"]);
// Defined custom routes by using "ng-view" directive,
// enable single page application (SPA) functionality
app.config(function ($routeProvider) {
$routeProvider
// Templates for WorkOrderDetail
.when("/", { templateUrl: "Template?name=Main", controller: "DetailController", controllerAs: "vm" })
.when("/Forms", { templateUrl: "Template?name=Forms", controller: "DetailController", controllerAs: "vm" })
.when("/Equipment", { templateUrl: "Template?name=Equipment", controller: "DetailController", controllerAs: "vm" })
.when("/JobPlan", { templateUrl: "Template?name=JobPlan", controller: "DetailController", controllerAs: "vm" })
.when("/AssetGroup", { templateUrl: "Template?name=AssetGroup", controller: "DetailController", controllerAs: "vm" })
.when("/Documents", { templateUrl: "Template?name=Documents", controller: "DetailController", controllerAs: "vm" })
.when("/History", { templateUrl: "Template?name=History", controller: "DetailController", controllerAs: "vm" })
.when("/LabourAndMaterial", { templateUrl: "Template?name=LabourAndMaterial", controller: "DetailController", controllerAs: "vm" })
.when("/Remedy", { templateUrl: "Template?name=Remedy", controller: "DetailController", controllerAs: "vm" })
.when("/Cause", { templateUrl: "Template?name=Cause", controller: "DetailController", controllerAs: "vm" })
// Templates for WorkOrderDetail Actions
.when("/AddRemedyCause", { templateUrl: "Action?name=AddRemedyCause", controller: "ActionController", controllerAs: "vm" })
.when("/AddResolutionNote", { templateUrl: "Action?name=AddResolutionNote", controller: "ActionController", controllerAs: "vm" })
.when("/ChangeLaborMaterial", { templateUrl: "Action?name=ChangeLaborMaterial", controller: "ActionController", controllerAs: "vm" })
.when("/ChangeStatus", { templateUrl: "Action?name=ChangeStatus", controller: "ActionController", controllerAs: "vm" })
.when("/ChangeWorkOrder", { templateUrl: "Action?name=ChangeWorkOrder", controller: "ActionController", controllerAs: "vm" })
.when("/CompleteWorkOrder", { templateUrl: "Action?name=CompleteWorkOrder", controller: "ActionController", controllerAs: "vm" })
.when("/ExtendCompletionDate", { templateUrl: "Action?name=ExtendCompletionDate", controller: "ActionController", controllerAs: "vm" })
.when("/ReAssign", { templateUrl: "Action?name=ReAssign", controller: "ActionController", controllerAs: "vm" })
.when("/ReDispatch", { templateUrl: "Action?name=ReDispatch", controller: "ActionController", controllerAs: "vm" })
.when("/UploadDocuments", { templateUrl: "Action?name=UploadDocuments", controller: "ActionController", controllerAs: "vm" })
.otherwise({ redirectTo: "/" });
});
Is there any way to cache all these templates BEFORE the person with a tablet (accessing this web app) goes offline?
Its a SPA on a client side. So, if it goes offline, these templates will not load. I need to cache them before we go offline.
Any suggestions how to make this work, preload and cache all these templates?
I need to enable offline capabilities for my app.

$urlRouterProvider $stateProvider otherwise does not work

Hi below is my code for urlRouterProvider otherwise.
.config(function config($urlRouterProvider, $stateProvider) {
$urlRouterProvider.otherwise('/invalid');
$stateProvider.state('view', {
url: '/view?{businessId:^[0-9]{1,20}$}',
templateUrl: 'view/view.tpl.html',
controller: 'ViewCtrl',
controllerAs: 'vm'
})
.state('otherwise', {
url: '/invalid',
templateUrl: 'view/view.error.tpl.html'
});
})
This is what i am expecting:
1. if i go to view?businessId=12345 it should load the page without any issues
2. if i go to view?businessId=abcd it should re-direct it to the view.error.tpl.html
But instead, it does nothing if i enter the invalid url.
I reffered to this post for help but none of the options really work Otherwise on StateProvider
you can try this, without using the otherwise function:
$stateProvider.state('view', {
url: '/view?{businessId:^[0-9]{1,20}$}',
templateUrl: 'view/view.tpl.html',
controller: 'ViewCtrl',
controllerAs: 'vm'
})
.state('otherwise', {
url: '*path',
templateUrl: 'view/view.error.tpl.html'
});
Try this
.config(function ($urlRouterProvider, $stateProvider) {
$stateProvider.state('view', {
url: '/view?{businessId:^[0-9]{1,20}$}',
templateUrl: 'view/view.tpl.html',
controller: 'ViewCtrl',
controllerAs: 'vm'
})
.state('otherwise', {
url: '*path',
templateUrl: 'view/view.error.tpl.html'
});
})

AngularJS : angular-ui-router state.go fails

on an Ionic-angular application, I'm calling:
$state.go("main.crudlist");
and having the following exception in response:
Error: Could not resolve 'entry' from state 'main'
at Object.transitionTo (angular-ui-router.min.js:3074)
at Object.go (angular-ui-router.min.js:3007)
at angular-ui-router.min.js:4057
at angular.min.js:138
at e (angular.min.js:40)
at angular.min.js:44
What's that entry supposed to mean?
Here's the state declaration:
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'Content/Mobile/templates/login.html',
controller: 'LoginController'
})
.state('settings', {
url: '/settings',
templateUrl: 'Content/Mobile/templates/settings.html',
controller: 'SettingsController'
})
// setup an abstract state for the tabs directive
.state('main', {
url: "/main",
templateUrl: "Content/Mobile/templates/main.html",
abstract: true,
controller: 'MainController'
})
.state('main.home', {
url: '/home',
views: {
'main': {
templateUrl: 'Content/Mobile/templates/home.html',
controller: 'HomeController'
}
}
})
.state('main.settings', {
url: '/settings',
views: {
'main': {
templateUrl: 'Content/Mobile/templates/settings.html',
controller: 'SettingsController'
}
}
})
.state('main.crudlist', {
url: "/crudlist",
views: {
'main': {
templateUrl: "Content/Mobile/templates/crudlist.html",
controller: 'CrudListController'
}
}
})
In the end,it had nothing to do to angular,but to the ionic html declaration itself.
I had:
<a ui-sref="entry" class="item" ng-repeat="leaf in menuleafs()" ng-click="loadApplication(leaf)">{{leaf.title}}</a>
Switching it to:
<a class="item" ng-repeat="leaf in menuleafs()" ng-click="loadApplication(leaf)">{{leaf.title}}</a>
Did the trick.
Sorry I didn't provide enough info

Resources