Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass param from state to state with angularJs?

I wish to pass a param from one store-state to the display product info in products-state:

My app - storeApp

.config(['$stateProvider', function($stateProvider) {
$stateProvider
    .state('store', {
        url: '/store',
        templateUrl: 'store/store',
        controller: 'storeCtrl'
    })
    .state('products', {
        url: '/products/:productSku',
        templateUrl: 'store/product',
        controller: 'productCtrl',
        resolve: {
                productResource: 'productFactory',
                _product: function(productResource, $stateParams){
                return productResource.getProduct($stateParams.productSku);
                    }
                }

Store.jade

a(href='/products/{{product.sku}}')

Product controller

.controller("productCtrl", function ($rootScope, $http, $stateParams, productFactory, storeFactory) {
//.controller('productCtrl', ['_product', function ($scope, $rootScope, storeFactory, _product) {

console.log($stateParams.productSku);

Product Factory

function getProduct(sku) {
    return $http.get('http://localhost:3000/api/products/' + sku );
}

Since I am using MEAN Stack, node has the router attached to express:

Server.js

const storeController = require('./controllers/store');
server.get('/store/product', passportConfig.isAuthenticated, storeController.getProductPage);

Store.js

exports.getProductPage = (req, res) => {
    res.render('store/product', {
        title: 'PP',
        angularApp: 'storeApp'
    })
}

I tried returning _product but I get Unknown provider: _productProvider <- _product <- productCtrl

I tried using ui-sref - a(ui-sref="products({productSku:'{{product.sku}}'})") in store.jade to send param from store_State to products_State & finally got an object back from API.

Now the issue is that node will not return the view.

Basically what I am trying to achieve is: Node serving client views, all store views - store/ product/ cart are attached to angular app served through Server.js, Clicking store product will redirect to product page after resolve product info from api. I am getting product info but not getting product view.

I looked it up but all solutions did not work....maybe my bad :-(
How can I go about this?

UPDATE-1: this is whats happening: App loads product but does not transfer to view

UPDATE-2: When I pass the control to angular, I have express routing the menu, and angular stateProvider routing/ connecting views to controllers. Main view that loads is the store itself:

app.js - store route

$stateProvider
    .state('store', {
            url: '/store',
            templateUrl: 'store/store',
            controller: 'storeCtrl'
    })

server.js (express)

server.get('/store', passportConfig.isAuthenticated, storeController.getStorePage);

store.js

exports.getStorePage = (req, res) => {
    res.render('store/store', {
        title: 'S--tore',
        angularApp: 'storeApp'
    });
}

store.ctr.js

angular.module("storeApp")
.controller("storeCtrl", function($rootScope, $http, storeFactory) {

    var products;

    storeFactory.getProducts().then(function(_products) {
        products = _products.data;
        $rootScope.products = products;
    });

That loads just fine!

But when I try to send the param productSku from store view to product view and have the resolve send product params back to product view that where it stops working, it's either I get the view OR i get the params.

WebStorm console log snapshot

I tried different ways of resolve, they all result the same - view OR product params.

app.js - product route

.state('products', {
    url: '/products/:productSku',
    templateUrl: 'store/product',
    controller: 'productCtrl',
    resolve: {
        _product: function ($stateParams, $state, $http) {
        return $http.get('http://localhost:3000/api/products/' + $stateParams.productSku );
        //return productResource.getProduct($stateParams.productSku)
        }
    }
})

If I remove the resolve and send a(href='/products/{{product.sku}}') from store.jade I get the template in the route, chrome console error I get is `Error: $injector:unpr Unknown Provider _product <- productCtrl

product.ctr.js

.controller('productCtrl', ['_product', function ($rootScope, $http, $stateParams, productFactory, storeFactory, _product) {

If I send a(ui-sref="products({productSku: product.sku })") with resolve I get product params (shown in WebStorem snapshot above) NO view.

like image 685
Jadeye Avatar asked Aug 07 '16 12:08

Jadeye


People also ask

What is params in AngularJS?

Angularjs routeParams is a service that allows you to retrieve the current set of route parameters(URL Parameters). It Requires the ngRoute module to be installed. The route parameters are a combination of $location's search() and path(). The path parameters are extracted when the $route path is matched.

What is state params AngularJS?

What Is the $stateParams Service in Angular? $stateParams is a service that captures URL-based parameters, and we can use these parameters to display information according to the state. Let's create an example with two states, understand how states work, and use $stateparams to store parameters.

What is stateParams?

$stateParams captures url-based params that $state considers applies to that state, even if its child state contains more params. $state. params seems to capture all url + non-url based params of the current state you are in. If you are in state parent.

What is state in AngularJS?

The STATE in AngularJS is a mechanism that allows us to update the view based on changes to the model. It is a two-way binding between data and DOM elements. Moreover, the State helps us keep track of data that changes over time, such as whether a particular button has been pressed or not.

How to pass route params in angular?

Angular Route Params: How to Pass Route Params in Angular. To access route parameters and query parameters in Angular, use ActivatedRoute service. The ActivatedRoute service provides Observables through which we can subscribe to the values of route params and route query params. In Angular, we can pass the data as route parameters in two ways.

What is the $stateparams service in angular?

What Is the $stateParams Service in Angular? $stateParams is a service that captures URL-based parameters, and we can use these parameters to display information according to the state. Let’s create an example with two states, understand how states work, and use $stateparams to store parameters.

How to pass values from one view to another in angular?

In Angular, we can pass the data as route parameters in two ways. Route params(Route parameters) Route queryParams(Route query parameters) If we want to pass values between views, then we can use route params. For example, if we’re going to pass an ID from one route to another and fetch the id on a component onInit(), then we can use route params.

How to pass a parameter from one state to another state?

There are 2 ways by that you can pass parameter from one state to another state. (i) Passing single Params using UI router. Js:- Let's we have templateUrl of our html file and EditApiCtrl is attached to it.Then we can pass parameter in this way.


1 Answers

angular will not load jade templates, You will need an html template, The jade template is loaded by express. You might like to try using ui-view like this:

Store.jade

div(ui-view)
  a(href='/products/{{product.sku}}')

Which should make angular look for the unnamed view when loading the route.

Your templateUrl's don't look to be pointing to files, perhaps you're missing the file extension?

Make sure you return a $promise in resolve as ui-router waits until they are resolved before rendering the view.

I'd recommend having named views with corresponding config in route too:

.state('store', {
    url: '/store',
    views: {
        '@': {
            templateUrl: 'store/store.html',
            controller: 'storeCtrl'
        }
    }
})
.state('products', {
    url: '/products/:productSku',
    templateUrl: 'store/product',
    controller: 'productCtrl',
    resolve: {
        _product: function ($stateParams, $state, $http) {
            return $http.get('http://localhost:3000/api/products/' + $stateParams.productSku ).$promise;

    }
}

})

See the docs here: https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views

like image 185
Adam Spence Avatar answered Oct 23 '22 12:10

Adam Spence