Create dynamic views based on ngRoute and Restlike API


John MacDonald

I have built a small HTML page with an ngView directive, after calling the Restlike API, the page is updated when any navigation link is clicked. This seems to work well, especially when there are no parameters to use in the request.

As soon as I try to add the parameter, it fails with the following error:

 TypeError: undefined is not a function
at Object.<anonymous> (http://localhost:8080/dashboard/js/factorys.js:11:16)
at Object.invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:3965:17)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:3807:37
at getService (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:3929:39)
at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:3956:13)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:3976:23)
at $get (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:7315:28)
at link (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular-route.js:907:26)
at nodeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:6752:13)
at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.js:6146:13) <div class="container ng-scope" ng-view="">

My angular script looks like this:

controllers.js:

var dashboardControllers = angular.module('dashboardControl',['ngRoute','dashboardFactory']);
dashboardApp.controller('PlayersController', function ($scope, $routeParams, Player) {
    $scope.players = Player.findAll();
});
dashboardApp.controller('PlayerDetailsController', function ($scope, $routeParams, Player) {
    $scope.playerId = $routeParams.playerId;
    $scope.player = Player.get();
});
dashboardApp.controller('OverviewController', function ($scope, $routeParams, Overview) {
    $scope.overview = Overview.query();
});

factory.js:

var dashboardFactories = angular.module('dashboardFactory',['ngResource']);
dashboardFactories.factory("Overview", ['$resource',
    function ($resource) {
        return $resource('webresources/overview/', {}, {
            query: {method: 'GET', isArray: false}
        });
    }]);
dashboardFactories.factory("Player", ['$resource',
    function ($scope, $resource) {
        return $resource('webresources/player/:playerId', {}, {
            findAll: {method: 'GET', isArray: true},
            get: {method: 'GET', params: {playerId: $scope.playerId}, isArray: false}
        });
    }]);

Outlining the work of the controller, my player controller seems to have an error message.

Radim Kohler

I have created working example here .

Note: it uses UI-Router instead of ngRoute...I guess this is indeed the correct way...but the concept is exactly the same.

First, we'll define the resource "player":

.factory('player', ['$resource', function($resource){

   return $resource('player:playerId.json', {}, {
     findAll: {method: 'GET', isArray: true},
     get: {method: 'GET', isArray: false}
    });

}]) 

Why do we use url like this ? Just for being chunky. We benefit here from parameterized URL templates ..., allowing each player in the template to have a list .player:playerId.jsonplayer.jsonplayer + playerId +.json

Country Definition:

// States
$stateProvider
  .state('list', {
      url: "/list",
      templateUrl: 'tpl.list.html',
      controller: 'PlayerListCtrl', 
      resolve : { 
        list : ['player', function(player){return player.findAll();}]
      }
  })
  .state('player', { 
      url: "/player/:playerId",
      templateUrl: 'tpl.player.html', 
      controller: 'PlayerCtrl', 
      resolve : { 
        player : ['player', '$stateParams'
        , function(player, $stateParams){
          return player.get({playerId: $stateParams.playerId});
        }]
      }
  })

The most important part is:

player : ['player', '$stateParams'
, function(player, $stateParams){
  return player.get({playerId: $stateParams.playerId});
}]

Because we let the angle infuse it $stateParamsinto our resolve. Then, we pass the playerIdparameters to the templated URL - which results in player1.json, player2.json...

In real life its url would be something like this "server/api/resource/:id" - but the logic is the same.

These are the consumption controllers:

.controller('PlayerListCtrl', ['$scope', 'list', function ($scope, list) {
  $scope.list = list;
}])
.controller('PlayerCtrl', ['$scope', 'player', function ($scope, player) {
  $scope.player = player;
}])

check here

Related


Create dynamic views based on ngRoute and Restlike API

John MacDonald I have built a small HTML page with an ngView directive, after calling the Restlike API, the page is updated when any navigation link is clicked. This seems to work well, especially when there are no parameters to use in the request. As soon as

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Create dynamic views in Android?

Teff In my application, I receive JSON multiple forms like this: "forms":[ {"name": "form1","title": "First Form","order": 1, "fields":[ "name": "firstName","type": "InputText", "maxLength": "10", "restriction

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Make (create) reusable dynamic views

Sane Developer The team wanted to create reusable, styleable views. For example, we want to reuse in different applications CommonPromptView(our own customizable dialog where we can hide the "Cancel" button, set a title, show a specific icon, etc.). There are

How to create dynamic views in Angular

lucky I want to dynamically create multiple horizontal lines using data obtained from a json file as shown in the image below. I try to do this component.html <div style="text-align: center"> <div class="flex-items"> <div *ngFor="let item of timelineObje

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

Create API endpoints to get dynamic data based on time

Jude Fernandes I have a scraper that periodically scrapes articles from news sites and stores them in a database [MYSQL]. The crawling is done by scraping the oldest articles first and then introducing the most recent articles. For example , articles written o

AngularJS multiple dynamic views based on single route

Mike DeMiller Forgive me if I don't explain this well. I'm having a hard time identifying the real problem I'm having. I'm hoping someone can understand what I'm thinking and maybe correct my misunderstanding of MVC, how Angular works or how controllers are st

Dynamic URL routing for class-based views

inverted grammar I want to convert a function view to a class based view. This is the view and URL path I have now. view: def customer(request, pk): customer = Customer.objects.get(id=pk) return render(request, 'accounts/customer.html) URL path: path(

Create dynamic views with static HTML in MVC

Robbye Rob I've been looking into dynamic content for MVC views and partial views but have not been successful in finding an architecture that suits my needs. Basically, I need to create a login page based on the parameters passed by the URL. For the basics, v

iOS: Create multiple dynamic views in interface builder

username How to create a view with dynamic height based on constraints? Here is what I am trying to achieve: I set constraints for each view, butExcept for height , since height should be created dynamically . Is there a way to achieve this without writing cod

Create dynamic views with static HTML in MVC

Robbye Rob I've been looking into dynamic content for MVC views and partial views but have not been successful in finding an architecture that suits my needs. Basically, I need to create a login page based on the parameters passed by the URL. For the basics, v

How to create separate views based on website users

username I first created a project with code. I want to create two separate routes with separate layouts, and a shared database. Manage separation by providing links separate from startup and using user authentication [Authorize]with user identity to help cont

Create views with different types of adapters based on rows

Android lovers I'm trying to replicate this Google Now interface. I'm not worried about how the card feels, but I'm more worried about how the view is constructed. I'm thinking of using an adapter that returns a different layout corresponding to the row. But a

Create views with different types of adapters based on rows

Android lovers I'm trying to replicate this Google Now interface. I'm not worried about how the card feels, but I'm more worried about how the view is constructed. I'm thinking of using an adapter that returns a different layout corresponding to the row. But a

How to create separate views based on website users

username I first created a project with code. I want to create two separate routes with separate layouts, and a shared database. Manage separation by providing links separate from startup and using user authentication [Authorize]with user identity to help cont

Create dynamic sidebar based on hash

fl Suppose I have a set of hashes like this: @demo = [{"category"=>"history", "title"=>"world war 2"}, {"category"=>"history", "title"=>"world war 1"}, {"category"=>"chemistry","title"=>"chemistry experiments" }] and a sidebar like this: <ul class="page-sideb

Create dynamic sidebar based on hash

fl Suppose I have a set of hashes like this: @demo = [{"category"=>"history", "title"=>"world war 2"}, {"category"=>"history", "title"=>"world war 1"}, {"category"=>"chemistry","title"=>"chemistry experiments" }] and a sidebar like this: <ul class="page-sideb

How to create dynamic filters based on dynamic parameters?

Phil I'm having a hard time coming up with a solution to generate the necessary logic and output based on dynamic filtering. I'm using a script from Google Apps Script and am doGet()used to something like a webhook. Send parameters to this webhook and then fil