Routing Without Slash in AngularJS - angularjs

I am preparing a mobile page for a web site. When guest open a link, php will redirect him/her to mobile page with same url if him/her device is mobile.
There are some problem. The urls like these sitename.com/product-name-334 and sitename.com/category_name_c. They detect the page thanks of _c and product ids. But I dont know how can I provide it by angular?
I want to listen your suggests.

You should try ui-Router's $stateProvider.
StateProvider API Reference
First, you need to reference ui-Router in your index file.
Then, you can assign a state to a hyperlink by using data-ui-sref attribute.
<li>
<a data-ui-sref="product">
</a>
</li>
</ul>`
Then you need to assign a url, a template and a controller to the state.
angular.module('sspRouting', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('product',
{
url: "/productDetail/{productId}",
templateUrl: 'app/view/productTemplate.html',
controller: 'productController'
})
)}]);
Then inside the controller you can use $routeParams to get the id from the url and then build your html using the data of that specific product.
.controller('productController', function($scope, $http, $routeParams, productService) {
var productId = $routeParams.productId;
// your code here.
//example $scope.productDetail = productService.getProductDetail(productId);
});

Related

angular 1 ui router maintaining url param data between states

I'm working with an app on Angular 1 and Ui-router, and I am trying to make so that data in urls will be preserved between states. I have read about the queryParamsHandling:'preserve' feature on Angular 2.0. However I am currently stuck with Angular 1 and I need to resolve how to keep the url data the same between states.
One option I was considering was to preserve the url:params data between states was with the ui-sref, however so far unsuccessful.
Does anyone have good tips how to resolve this?
Thanks
Router File --> route.js:
angular
.module('moduleName')
.config(['$stateProvider', stateProvider])
function stateProvider($stateProvider) {
$stateProvider
.state('lessonDetails', {
url: '/:lessonId/details',
views: {
'content': {
templateUrl: 'lesson/lesson-details.html',
controller: 'lessonController'
}
}
});
}
HTML file --> lesson-details.html:
<a ui-sref="lessonDetails({'lessonId': 123})">Go to details</a>
Controller -- > lesson-details.js
angular
.module('moduleName')
.controller('lessonController', ['$scope', '$stateParams', lessonController]);
function lessonController($scope, $stateParams){
//use lessonId passed as params using $stateParams
console.log($stateParams.lessonId)
}

Remove url from end user in ui routing

I am building angular single page application with ui-routing. I need to hide the url after the domain from end user.It shuld not affect any other feature like accessing url or anything, but just want to hide the state changes from end user. Which is the best way to do that?
you can do that with ui-router library by creating the URL-less states and navigate to them ,like this:
angular.module('app', ['ui.router'])
.config(function ($stateProvider) {
$stateProvider
.state("home", {
url: "",
template: "<a ui-sref='next'>Next page</a>"
})
.state("next", {
template: "<a ui-sref='home'>Back</a>"
});
});

Angular routing not working for a link but working for all others

I got the routing working for all my links but one and don't understant what happens.
I include dependency to ui-router :
var app = angular.module('CMT', ['ui.router', 'angularCharts', 'uiSwitch']);
configure a new state :
app.config([
'$stateProvider',
'$urlRouterProvider',
function ($stateProvider, $urlRouterProvider){
$stateProvider.state('writeareview', {
url: '/#/writeareview',
templateUrl: 'partials/writeareview.html',
controller: 'writeAReviewController'
});
Declare the controller :
app.controller('writeAReviewController', ['$scope', function ($scope){
}]);
And my template is located in the folder "partials" with following code :
<div ng-controller="writeAReviewController"></div>
My link in index.html :
<li ng-class="getClass('/writeareview')">Donner un avis</li>
Any help would be much appreciated.
The routing url shouldn't have a hash in it... that is done internally.
Change:
url: '/#/writeareview'
To
url: '/writeareview'
And change the href to only include hash with no leading /:
<a href="#/writeareview">
Or use
ui-sref="writeareview"
Also you will be invoking 2 instances of your controller when you include the controller in routing and in ng-controller. Remove the ng-controller duplicate

$stateProvider does not redirect. ngRoute was working fine. But had to switch to angular-ui-router for multiple and nested views loading

Developing a POC for an application.
Technologies being used : ASP.Net MVC4 and AngularJs.
Initialy while getting started i was using ngRoute and $routeProvider. The links used to redirect to the server to the required controller action and pages used to load in the div marked with ng-view directive. But my requirement was to laod multiple and nested views. So, had to switch to $stateProvider which supports such requirements.
Now i am facing the issue of the links not getting redirected. The "Project" link redirects but the "Opportunity" does not. My requirement is to load a view inside the first div and then one more view into AddOpportunityContainer
Following below is the code which i had written. Can anyone help me what wrong i have been doing. Had already spent quite an amount of tme on it.
Referred Libraries:
angular.js and angular-ui-router.js
The Code:
var GuidanceApp = angular.module('GuidanceApp', ['ui.router']);
var configFunction = function ($stateProvider, $httpProvider, $locationProvider) {
$stateProvider
.state('Projects', {
url: '/Projects',
templateUrl: 'Projects/Index'
})
.state('Opportunity', {
url: '/Opportunity',
templateUrl: '/Opportunity/Index',
views:{
"AddOportunityContainer": {
templateUrl: '/Opportunity/Create'
}
}
});
}
configFunction.$inject = ['$stateProvider', '$httpProvider', '$locationProvider'];
GuidanceApp.config(configFunction);
Following is the HTML of home page. And as you can see, the last div is marked with ui-view now instead of ng-view as in case of ngRoute and $routeProvider
<div>
<ul>
<li>Projects</li>
<li>Opportunity</li>
<li>Adjustments</li>
<li>Summary</li>
</ul>
</div>
<div ui-view></div>
Different partial view to be loaded on click of Opportunity link.
<h2>Opportunity-Index</h2>
<div ui-view="AddOportunityContainer"></div>
Your link for anchor element should be corrected as below without leading '/':
<li>Projects</li>

refresh button is killing the controller

I have a problem between my template and controller. When i hit the refresh button on the web browser my controller is getting destroyed and not getting created again. So view doesnt function. Is there any way to avoid this error ?
UPDATE :
It was an other service which caused ctrl not the work. I solved the problem but still i couldnt figure out how to manipulate the URL when a person presses refresh in the browser..
Try this:
.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
// For unmatched routes
$urlRouterProvider
.when('', '/home')
.otherwise('/errorPage');
// Application routes
$stateProvider
.state('someState', {
url: '/somePage',
controller: 'SomeCtrl',
templateUrl: 'any.html',
})
.state('otherOne', {
url: '/otherPage',
templateUrl: 'other.html',
})
}
And to shitch between states use <a ui-sref="someState">cLick Me</a>

Resources