Passing parameters from input bar - angularjs

I want to create an input bar where I can submit zip code that I later use to find pizzerias that deliver to that zip code. Queries aside, I have a problem with sending the zipCode. Any suggestions?
home.html
<div class="bar">
<form ng-submit=submit() ng-controller="zipCodeController">
<p><b>Wpisz kod pocztowy np. 30-069</b></p>
<input type="text" ng-model="text" name="text"/>
</form>
</div>
main.js
app.controller('zipCodeController', ['$scope', $location, function($scope, $location) {
$scope.zipCode = '';
$scope.text = 'Wpisz adres pocztowy np. 30-069';
$scope.submit = function () {
if ($scope.text) {
$scope.zipCode.push(this.text);
$scope.text = '';
}
$location.path('/pizzeriaList/'+$scope.text);
}
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
// Home
.when("/", {templateUrl: "partials/home.html", controller: "PageCtrl"})
// Pages
.when("/about", {templateUrl: "partials/about.html", controller: "PageCtrl"})
.when("/contact", {templateUrl: "partials/contact.html", controller: "PageCtrl"})
.when("/pizzeriaList", {templateUrl: "partials/pizzeriaList.html", controller: "PageCtr"})
.when("/p/:id", {templateUrl: "partials/pizzeria.html", controller: "PageCtrl"})
.when("/p/:id/:pid", {templateUrl: "partials/addPizza.html", controller: "PageCtrl"})
.when("/cart", {templateUrl: "/partials/cart.html", controller: "PageCtrl"})
.when("/pizzeriaList/:zipCode", {templateUrl: "/partials/pizzeriaList.html", controller: "PizzeriaListCtrl"})
.otherwise("/404", {templateUrl: "partials/404.html", controller: "PageCtrl"});
app.controller('PizzeriaListCtrl', function($scope){
$scope.zipCode = $routeProvider.zipCode;
})

This is what services are for, they can be injected into each component that needs to share data.
app.service('zipCodeService', function() { return { zipCodes:[] }; });
add it to your dependency list in your component
app.controller('zipCodeController', ['$scope', '$location', 'zipCodeService', function($scope, $location, zipCodeService)
and now add zipCode to the service not your scope
zipCodeService.zipCodes.push(this.text);
Any other component you want to access that array from you inject the service and it is the same instance of the array.

Related

AngularJS routeParam not working

I have url like this:
http://localhost:3000/details/59567bc1de7ddb2d5feff262
and I want to get the parameter id
59567bc1de7ddb2d5feff262
But for some reasons routeParam always returns undefined
My controller is:
task.controller('ctrla', function($rootScope, $scope, $http, $timeout, $routeParams){
$scope.first = 1;
console.log($routeParams);
});
routes :
task.config(function ($routeProvider, $locationProvider){
$routeProvider.when('/details/:id', {
templateUrl: "details.html",
controller: "ctrla"
})
});
any help will be a life save.
Make sure you have defined the route urls as mentioned below,
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
template: "HI this is home Screen",
controller: 'ctrla'
})
.when('/details/:id', {
templateUrl: "template.html",
controller: 'profileController'
})
.otherwise({
redirectTo: '/home'
})
}]);
DEMO
If yu have route deinition defined as
$routeProvider.when('/details/:id', {
templateUrl: "partial.html",
controller: "ctrla"
})
Then in controller you should get its value as
task.controller('ctrla', function($rootScope, $scope, $http, $timeout, $routeParams){
$scope.first = 1;
console.log($routeParams.id);
});

AngularJS UI-Router one view utilised by multiple controllers separately

I didn't exactly found an answer on this specific question here on stackoverflow.
In UI-Router can and may i specify the same view/templateUrl for 2 different states?
.state('app.dashboardLanding', {
url: '/dashboard-landing',
title: 'Dashboard Overview',
templateUrl: 'app/views/landing.html',
controller: 'DashboardLandingController',
})
.state('app.balanceSheetLanding', {
url: '/balance-sheet-landing',
title: 'Balance sheet Overview',
templateUrl: 'app/views/landing.html',
controller: 'BalanceSheetLandingController'
})
the code in app/views/landing.html is exactly the same, only the $scope properties are different.
app.js
'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
$routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'homeCtrl'});
$routeProvider.when('/salesnew', {templateUrl: 'partials/salesnew.html', controller: 'salesnewCtrl'});
$routeProvider.when('/salesview', {templateUrl: 'partials/salesview.html', controller: 'salesviewCtrl'});
$routeProvider.when('/users', {templateUrl: 'partials/users.html', controller: 'usersCtrl'});
$routeProvider.when('/forgot', {templateUrl: 'partials/forgot.html', controller: 'forgotCtrl'});
$routeProvider.otherwise({redirectTo: '/login'});
}]);
app.run(function($rootScope, $location, loginService){
var routespermission=['/home']; //route that require login
var salesnew=['/salesnew'];
var salesview=['/salesview'];
var users=['/users'];
$rootScope.$on('$routeChangeStart', function(){
if( routespermission.indexOf($location.path()) !=-1
|| salesview.indexOf($location.path()) !=-1
|| salesnew.indexOf($location.path()) !=-1
|| users.indexOf($location.path()) !=-1)
{
var connected=loginService.islogged();
connected.then(function(msg){
if(!msg.data)
{
$location.path('/login');
}
});
}
});
});

ng-view and routing is not working

I've been struggling with using ng-view for my subview of my application. I've followed multiple tutorials and have even checked the routeProvider and locationProvider attributes to ensure that they were pointing to the correct path.
index.html
<html ng-app="elephantApp">
...
<div id="view" ng-view>{{$scope.message}}</div>
...
</html>
application.js
/*
Main AngularJS for Elephant Blog App
*/
var elephantApp = angular.module("elephantApp", ["ngRoute", "ui.bootstrap"]);
elephantApp.config(function ($routeProvider, $locationProvider){
$routeProvider
// Home
.when('/', {
templateUrl: "app/partials/home.html",
controller: "ElephantController"
})
.when('/about', {
templateUrl: 'partials/about.html',
controller: 'PageCtrl'
}) ;
$locationProvider
.html5Mode(true);
});
elephantApp.controller("ElephantController", function($scope, $route, $routeParams, $location){
$scope.contentClass = 'content-home';
$scope.message = {message: "Hi"};
});
elephantApp.controller("PageCtrl", function($scope, $route, $routeParams, $location){
$scope.contentClass = 'content';
$scope.model = {message: "Hey!"};
});
/*
function ElephantController($scope, $route, $routeParams, $location){
$scope.contentClass = 'content-home';
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
$scope.model = {message: "Hey"};
}
*/
So I have no idea what's going on at all.
I have even tried to do code like:
// Pages
.when("/about", {templateUrl: "partials/about.html", controller: "PageCtrl"})
.when("/faq", {templateUrl: "partials/faq.html", controller: "PageCtrl"})
.when("/contact", {templateUrl: "partials/contact.html", controller: "PageCtrl"})
// Blog
.when("/blog", {templateUrl: "partials/blog.html", controller: "BlogCtrl"})
.when("/blog/post", {templateUrl: "partials/blog_item.html", controller: "BlogCtrl"})
// else 404
.otherwise("/404", {
templateUrl: "partials/404.html",
controller: "PageCtrl"
});
I would like to use the above code as it is what I mainly want to do. Though it isn't working. The ng-includes work but not my ng-views. Am I missing something?
Thanks.
By default, a route in angular is the hash in the URI path after #, for example http://app/#/about. By setting $locationProvider.html5Mode to true how you done, we can write without # http://app/about, but need that server always returning index.html. For this purposes you can use, for example, Express server: http://www.seankenny.me/blog/2013/08/05/angularjs-in-html5-mode-with-expressjs/

AngularJS ng-class active by any subroute

I have a tab I want to be activated by a link like this:
http://localhost:8020/client#/content
Therefore I have this list-element:
li ng-show="showContentItemsTab" ng-class="{active: isActive('/content')}">Content</li>
But I also want it to be active when sublinks are called:
http://localhost:8020/client#/content/531443caeb3f95600ef92e3f
Is there a way to apply all sublinks after /content ?
Something like:
li ng-show="showContentItemsTab" ng-class="{active: isActive('/content/*')}">Content</li>
Following the official angular js tutorial, you can set up routing as follows.
phonecatApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
when('/phones/:phoneId', {
templateUrl: 'partials/phone-detail.html',
controller: 'PhoneDetailCtrl'
}).
otherwise({
redirectTo: '/phones'
});
}]);
The controller will be
var phonecatControllers = angular.module('phonecatControllers',[]);
phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
$scope.phone = data;
});
}]);
So your can be fetched by using $routeParams

angular dynamic templateUrl

Hi have one more question, I have next config for angular:
angular.module('ow', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/:placeId', {templateUrl: 'partials/menu.html', controller: MenuCtrl}).
when('/menu/:itemId', {templateUrl: 'partials/menu-details.html', controller: MenuItemCtrl}).
when('/look/refill', {templateUrl: 'partials/refill.html', controller: RefillCtrl}).
when('/look/orderCart', {templateUrl: 'partials/orderCart.html', controller: OrderCartCtrl}).
when('/lang/:lang', {templateUrl: 'partials/menu.html', controller: LangCtrl}).
when('/waiter/:redirect', {templateUrl: "???????", controller: WaiterCtrl}).
otherwise({redirectTo: '/0'});
}];
Instead of "?????" I need to put dynamic url, tried to do it in controller like:
function WaiterCtrl($routeParams, $location, sharedData, $http, $route) {
$http.get(config.urls.ajaxWaiter + "{\"p\":\"" + sharedData.getOrderCart().orderPlace + "\"}").success(function(dataDetails) {
if ($routeParams.redirect == "menu") {
$route.templateUrl = "partials/menu.html";
$location.path("/");
}
if ($routeParams.redirect == "menuDetails") {
$route.templateUrl = "partials/menu-details.html";
$location.path("/menu/" + sharedData.getMenu());
}
if ($routeParams.redirect == "orderCart") {
$route.templateUrl = "partials/orderCart.html";
$location.path("/orderCart");
}
if ($routeParams.redirect == "refill") {
$route.templateUrl = "partials/refill.html";
$location.path("/refill");
}
return $route.templateUrl;
});
}
but it doesn't work... Can you help me?
You won't be able to put dynamic code into the routing. This is because the routing happens during the Config phase, which is executed before Angular starts running your application.
I think the easiest or cleanest way to do what you are trying to do is just have an inline controller in the route definition. I set up a simple plunk to show redirecting inside the route definition: http://plnkr.co/edit/aeSjmn?p=preview
Here's some sample code that might work for you:
angular.module('ow', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/:placeId', {templateUrl: 'partials/menu.html', controller: MenuCtrl}).
when('/menu/:itemId', {templateUrl: 'partials/menu-details.html', controller: MenuItemCtrl}).
when('/look/refill', {templateUrl: 'partials/refill.html', controller: RefillCtrl}).
when('/look/orderCart', {templateUrl: 'partials/orderCart.html', controller: OrderCartCtrl}).
when('/lang/:lang', {templateUrl: 'partials/menu.html', controller: LangCtrl}).
when('/waiter/:redirect', {template: '', controller: function ($scope, $routeParams, $location) {
function WaiterCtrl($routeParams, $location, sharedData, $http, $route) {
$http.get(config.urls.ajaxWaiter + "{\"p\":\"" + sharedData.getOrderCart().orderPlace + "\"}").success(function(dataDetails) {
if ($routeParams.redirect == "menu")
$location.path("/");
else
$location.path("/" + $routeParams.redirect);
})
}}).
otherwise({redirectTo: '/0'});
}];

Resources