Exploring dynamic page linking with ui.router in Angular

In the process of developing a backend rails application that generates an API for consumption by a front end ionic app, I encountered an issue. Despite having experience with Angular, my familiarity with Ionic is limited. The specific problem lies in linking to the show pages of a static resource received from the API.

The index action works smoothly, allowing me to list the resources. However, attempting to link to the show pages results in a blank white screen. Upon inspecting the angular show controller using console logs, I confirmed that the controller is running but fails to link to the page. Below is the configuration setup:

app.js

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'AppCtrl'
  })

.state('app.search', {
    url: "/search",
    views: {
      'tab-search': {
        templateUrl: "templates/search.html"
      }
    }
  })

.state('app.gyms', {
    url: '/gyms',
    views: {
      'tab-gyms': {
        templateUrl: 'templates/gyms.html',
        controller: 'GymsIndexCtrl'
      }
    }
  })

  .state('app.gymShow', {
    url: "/gyms/:gymId",
    views: {
      'tab-gyms': {
        templates: "templates/gym.html",
        controller: 'GymsShowCtrl'
      }
    }
  })

  $urlRouterProvider.otherwise('/app/search');
})

controllers

angular.module('myApp.controllers', [])
.controller('GymsIndexCtrl', function($scope, $stateParams, Gyms) {
  $scope.gyms = Gyms.index(function(response) {
    console.log("from gyms");
    console.log(response);
  });
})

.controller('GymsShowCtrl', function($scope, $stateParams, Gyms) {
  $scope.gym = Gyms.query({id: $stateParams.id}, function(response) {
    // this is what's running, and it works
    console.log("running");
    console.log(response);
  });
})

gyms.html

<ion-view view-title="Gyms">
  <ion-content>
    <h1 class="text-center">Gyms</h1>
    <hr class="generic">
    <div ng-repeat="gym in gyms">
        <div class="list card">
              <div class="item item-avatar">
                <h2 class="pull-left"><b>{{gym.name}}</b></h2>
                <p class="pull-lefft">{{gym.description}}</p>
              </div>

              <div class="item item-image">
                <img src="{{gym.images}}" style="min-height: 200px;">
              </div>

          <!-- where the linking is. I have tried routes like this, and the
          ui.router syntax of ui-sref="app.gymShow({gymId: gym.id})"
          neither produce and error, but neither work -->

              <a class="item item-icon-left assertive" href="#/app/gyms/{{gym.id}}">
                <ion-icon name="person"></ion-icon>
                {{gym.reviews_count}} reviews
              </a>
            </div>
        </div>
  </ion-content>
</ion-view>

The above code illustrates the set up used, similar to the ionic scaffold example. Despite following the template closely, the issue persists. Hence, I have two questions - how can I successfully link to show pages from an index action in ionic, and can I utilize $routeProvider in Ionic?

Your guidance on either of these queries would be greatly appreciated.

Answer №1

Well, I must admit that I made a really silly mistake here. Take a look at this snippet of code:

.state('app.gymShow', {
    url: "/gyms/:gymId",
    views: {
      'tab-gyms': {
        templates: "templates/gym.html",
        controller: 'GymsShowCtrl'
      }
    }
  })

I actually meant to change it to the following structure:

.state('app.gyms', {
    url: '/gyms',
    views: {
      'tab-gyms': {
        templateUrl: 'templates/gyms.html',
        controller: 'GymsIndexCtrl'
      }
    }
  })

It was just a simple syntax error on my part. Live and learn!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is Nextjs the best choice for developing the frontend code exclusively?

When deciding whether to use Next.js or plain React for my front-end development, should I consider that a back-end already exists? If I am not planning to write a new back-end, which option would be better suited for the project? ...

javascript variable pointing to an unknown object

Below is the essential code snippet to consider: JS function ToggleRow(objTwistie, objRow) { if (objRow.style.display == "none") { objRow.style.display = ""; objTwistie.src = "../Images/SectionDown.gif"; } else { objRow.style.display = "n ...

Issue with smart table sorting functionality

I've been working on a table with just one column that is populated from a string array. However, I'm struggling to get the sorting functionality to work properly. Can anyone pinpoint what mistake I might be making here? Steps taken so far: 1) ...

Prevent accordion expansion triggered by the More button click

When the More button is clicked, it should NOT expand and collapse. https://i.sstatic.net/toV1b.gif If the accordion initial state is open, it must stay open even if button is clicked, if initial state is collapsed then after the more button the accordio ...

Node JS promise predicaments

I'm stuck trying to figure out why this function is returning before my message array gets updated with the necessary values. var calculateDistance = function (message, cLongitude, cLatitude, cSessionID) { return new Promise(function (resolve, re ...

The $save function does not operate properly; although the call is successful, it fails to trigger the .then() method

Attempting to place a call that POSTs an array using $save, but encountering issues with callback function execution. Despite receiving a 200 response, both success and failure callbacks are not triggering. var endpoint = "my valid url"; var ...

Array of generic types in Typescript

Here's a method that I have: getFiveObjectsFromArray(array: T[]) { return array.slice(0, 5); } I've been using this method multiple times. Is there a way in TypeScript to pass a generic argument instead of using multiple types? Also, when ...

Building an NPM module specifically designed for Ionic app development requires a strategic approach

Currently, I am in the process of creating a library for Ionic 2 that needs to be installed via NPM. However, I am facing some challenges with the traditional installation method. When trying to develop a module, you can usually use the npm link command to ...

Issue with Material UI tool tip not closing upon clicking on an element is persistent

Check out this link for a material UI tooltip demo I have been experimenting with the material UI tooltip in the provided link. The tooltip pops up when hovering over the button, but it doesn't disappear when clicking on the button. Is this the defau ...

The most effective method for acquiring an object through inheritance

I'm seeking advice on the best practice for adding behavior to an object received as a JSON object. I have REST services that allow me to define a sort of state machine. The API defines a /sessions resources. When creating a session via POST /sessio ...

What is the reason behind the success of ajax POST compared to GET with Rails' json request?

Struggling to grasp the inner workings of ajax in relation to Rails. In a method of mine, depending on the selection made in an html select, I aim to load another select with the corresponding value from the first one. Noteworthy is that these selects are ...

Triggering JavaScript Function When Scrolling in Overflowed DIV

After using jQuery and searching for various ways to make this script work, I finally found a solution that works for my index.html file. <div style="overflow-y:scroll; height:300px"> <div style="background-color:black; height:500px"> </div ...

Ways to invoke a JavaScript function via a hyperlink. Various techniques to achieve this task

I want to create a div with the id of slider, and I am looking for a way to animate or hide it when a link is clicked. Specifically, I would like to toggle it open and close using a link. Javascript solution <script type="text/javascript"> ...

Utilizing AngularJS for Showcasing JSON Information Using Ng-Repeat and Ng-Factory

As a newcomer to Javascript and Angular, I am currently working on integrating AngularJS into my website. Despite watching tutorials from CodeSchool, Egghead, and others, I seem to be stuck at the very beginning. My issue lies in retrieving JSON data from ...

Utilizing ThemeProvider in a Different Component in React

In my App.js file, I have a function that toggles between light and dark mode themes: import { createTheme, ThemeProvider } from '@mui/material/styles' import Button from '@mui/material/Button' import Header from './components/Head ...

What is the best way to update the class names of all JSX elements within a component when new props are received?

I am currently working on developing a container component that will store filter data and pass it down to another component responsible for rendering the filtered list. The data for this container component is sourced from a parent component. The filterin ...

Saving the output of a function in Javascript/NodeJS to a variable

I've been attempting to save the output of a function into a variable, but it's not working as expected. I've exhausted all my ideas and possibilities. Perhaps I'm just making a silly mistake :D I'm working with NodeJS, using expre ...

How to Remove onFocus Warning in React TypeScript with Clear Input Type="number" and Start without a Default Value

Is there a way to either clear an HTML input field of a previous set number when onFocus is triggered or start with an empty field? When salary: null is set in the constructor, a warning appears on page load: Warning: The value prop on input should not ...

managing browser pop-ups in selenium with the help of JavaScript

My objective is to input a username and password in the popup box that shows up every time the page loads. I am utilizing selenium for this purpose, and unfortunately, all attempts I've made so far have been unsuccessful. I attempted to use the follo ...

"Expand" Button following X buttons

Check out this JSFiddle for the code: $(document).ready(function(){ $( ".keywordsdiv" ).each(function(){ $(this).children(".keywords").eq(3).after('<a href="" id="playtrailershowmorebuttons">....Show More</a>');//add a uniq ...