PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to create a multi-use partial "template" in AngularJS?

 May 18, 2022     angularjs, list, partial, templates, view     No comments   

Issue

I have a large list of items. Each item has it's own details.

In my main view/partial, I simply display a large list list of the item names. When the user clicks on an item, I want the page to go to a partial which works as a "template", displaying information based on which list item is clicked, and hence possibly what the URL looks like. E.g. /listItem1/

This diagram below hopefully sums up what I want to achieve pretty clearly. enter image description here

How can I do this?

Right now, I have a pretty standard set up in which I have all the information for each list item in an array of object literals, which is contained in a controller injected into the main app module. Like so:

    var app = angular.module('app', [/*nodependencies*/]);

    var controllers = {};

    app.controller(controllers);

    controllers.listController = function ($scope){ 
    $scope.list = [
            {name: 'List Item 1 Name',  detail1: 'blahblah1', detail2: 'blahblah2'},
{name: 'List Item 2 Name', detail1: 'blahblah1', detail2: 'blahblah2'},
{name: 'List Item 3 Name', detail1: 'blahblah1', detail2: 'blahblah2'}  
        ..... and so on

I know how to create basic views/partials as well. But what would be my next steps?


Solution

You can do what you want, using the built-in router which ships with AngularJS.

var app = angular.module('app', [/*nodependencies*/])

    .config(function($routeProvider) {
        $routeProvider
            .when('/:itemId', {
                templateUrl: '/path/to/partial',
                controller : function($scope, $routeParams) {
                    $scope.item = $routeParams.itemId;
                }
            })
    });

Basically, what the above means, is that if you browse to pdf/item/1

Then you will have access in your controller to $routeParams.itemId which will be equal to 1. You can then do whatever logic is necessary with this information on your partial to show the information you want.

Hope this helps.

Update

Please look at the controller, this is how you would get the param you passed via the URL, you would then do whatever it is you need to do with that param in the controller, and pass the data back to the view.



Answered By - francisco.preller
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing