Troubleshooting routing problems with AngularJS slider widget - struggling to navigate between pages/buttons efficiently

I am brand new to AngularJS and I am attempting to get a slider to function that I found in an online example. Currently, the slider displays on the desired page (gallery.html) and the automatic image change is working fine. However, when I try to click on the next/previous button, it redirects me to a random empty page.

I suspect the issue lies with the hrefs on the arrows, but I am unsure how to proceed from here. Additionally, is my slider directive correctly placed at the top of gallery.html?

File structure:

Photography
- bower_components
- css
----- stylemain.css
- img
----- phones
---------- ...a bunch of png files...
- js
----- app.js
----- controller.js
- partials
----- gallery.html
- phones
----- ...a bunch of json files...
- index.html

This is my index.html:

<!DOCTYPE html>
<html lang="en" ng-app="mainApp">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
    <!--<link rel="stylesheet" href="css/app.css">-->
    <link rel="stylesheet" href="css/stylemain.css">

    <!-- JS & ANGULAR FILES -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular-touch.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/1.10.3/TweenMax.min.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="https://code.angularjs.org/1.4.8/angular-touch.js"></script>
    <script src="https://code.angularjs.org/1.4.8/angular-animate.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controller.js"></script>
    <!--<script src="js/directives.js"></script>-->
</head>

<body>
    <div class="template-header">
        <div class="template-container">
            <div class="template-logo">
                <h1><a href="#/">title</h1>
            </div>
            <div class="template-nav">
                <ul>
                    <li><a href="#/">Home</a></li>
                    <li><a href="#/gallery">Gallery</a></li>
                    <li><a href="#/music">Music</a></li>
                    <li><a href="#/other-work">Other-work</a></li>
                </ul>
            </div>
        </div>
    </div>

    <!-- BODY CONTENT -->
<div class="dynamic-body" ng-view></div>

</body>

This is my app.js:

    'use strict';

/* App Module */

var mainApp = angular.module('mainApp', [
    'ngRoute',
    'galleryControllers'
]);

mainApp.config(['$routeProvider',
    function($routeProvider){
        $routeProvider
            .when('/', {
                templateUrl:'partials/main.html',
            })
            .when('/gallery', {
                templateUrl:'partials/gallery.html',
                controller: 'mainImageCtrl',
            })
            .when('/:phoneId', {
                templateUrl: 'partials/gallery-image.html',
                controller: 'singleImageCtrl'
            })
            .when('/music', {
                templateUrl: 'partials/music.html',
                controller: 'singleImageCtrl'
            })
            .when('/other-work', {
                templateUrl: 'partials/other-work.html',
                controller: 'singleImageCtrl'
            });
    }
]);

This is my controller.js:

'use strict';

/* Controllers */

var galleryControllers = angular.module('galleryControllers', [
    'ngAnimate'
]);

galleryControllers.controller('mainImageCtrl',['$scope', '$http',
    function($scope, $http){
        $http.get('phones/phones.json').success(function(data){
            $scope.images = data;
        });
}]);

galleryControllers.directive('slider', function($timeout) {
  return {
    restrict: 'AE',
    replace: true,
    scope: {
      images: '='
    },
    link: function(scope, elem, attrs) {
        scope.currentIndex=0;

        scope.next=function(){
            scope.currentIndex<scope.images.length-1?scope.currentIndex++:scope.currentIndex=0;
        };

        scope.prev=function(){
            scope.currentIndex>0?scope.currentIndex--:scope.currentIndex=scope.images.length-1;
        };

        scope.$watch('currentIndex',function(){
            scope.images.forEach(function(image){
                image.visible=false;
            });
            scope.images[scope.currentIndex].visible=true;
        });

        /* Start: For Automatic slideshow */

        var timer;

        var sliderFunc=function(){
            timer=$timeout(function(){
                scope.next();
                timer=$timeout(sliderFunc,2000);
            },2000);
        };

        sliderFunc();

        scope.$on('$destroy',function(){
            $timeout.cancel(timer);
        });

        /* End : For Automatic slideshow */

    }

  };
});



// galleryControllers.controller('singleImageCtrl',['$routeParams','$scope', 
//  function($scope, $routeParams){
//      $scope.phoneId = $routeParams.phoneId;
// }]);

This is my gallery.html:

<slider images="images"/>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-2">
      <!--Sidebar content-->

      Search: <input ng-model="query"/>
      Sort by:
      <select ng-model="orderProp">
        <option value="name">Alphabetical</option>
        <option value="age">Newest</option>
      </select>

    </div>

      <!-- Body content -->

 <div class="slider">
  <div class="slide" ng-repeat="image in images" ng-show="image.visible">
    <img ng-src="{{image.imageUrl}}" />
  </div>
  <div class="arrows">
    <a href="#" ng-click="prev()">
      <img src="img/left-arrow.png" />
    </a>
    <a href="#" ng-click="next()">
      <img src="img/right-arrow.png" />
    </a>
  </div>
</div>

  </div>
</div>

phones.json is simply a JSON file containing information about various phones.

Thank you in advance for any assistance provided!

Answer №1

Exploring https://github.com/angular-ui/ui-router is a great way to dive into routing functionality.

Each time you attempt to access a route, remember to use:

 <a ui-sref="root">link</a>

appModule.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/signin');
$stateProvider
    .state("root", {
        url: "/signin",
        templateUrl: "views/signin.html",
        controller: 'AuthController'
    }) 

By utilizing ui-sref="root," navigation to the designated route becomes seamless.

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 there a way to make the submit button navigate to the next tab, updating both the URL and the tab's content as well?

I am encountering an issue with my tabs for Step1 and Step2. After pressing the submit button in Step1, the URL updates but the component remains on tab1. How can I resolve this so that the user is directed to the Step2 tab once they click the submit butto ...

Leveraging React Native to position a view absolutely in the center of the screen without obstructing any other components

How can I center an image inside a view in the middle of the screen using position: "absolute"? The issue is that the view takes up 100% of the width and height of the screen, causing all components underneath it (such as input fields and buttons ...

Utilizing CSS with the data attribute within an <object> element

Can CSS be used on a data attribute of an object tab? Here is the code snippet (excluding attributes like type): <object data="www.example.com"> I am interested in styling "www.example.com" from my own site using CSS. The mobile site I am attemptin ...

Is there a way to ensure a box remains fixed on the edge of a browser window?

Looking to create a tooltip div that pops up next to and aligns with the top of a specific input element when it is clicked: lorem ipsum dolor ____________ ___________________ | this | [_button____clicked_] | appears | | on s ...

How to handle an unexpected keyword 'true' error when using the `useState` hook in React?

Trying to set the open prop of the MUIDrawer component to true on user click is causing an error stating "Unexpected keyword 'true'" import React, { useState } from "react"; import { withRouter } from "react-router-dom"; impo ...

Replace text using the str_replace function

I have a total of seven lines of text that I need to manipulate using PHP. My first task is to filter out any lines that do not contain the word 'MARCO1998'. Next, from the remaining lines, I need to extract only the 1-3 numbers that come after ...

How do I specify a unique directory for pages in Next.js that is not within the src or root folders?

I'm currently facing an issue when trying to set a custom directory in Next JS. Although the default setup dictates that the pages directory should be located at the root or within the src directory, this arrangement doesn't fit my requirements ...

Tips on organizing and designing buttons within a canvas

let canvas = document.getElementById("canvas"); let context = canvas.getContext("2d"); // for canvas size var window_width = window.innerWidth; var window_height = window.innerHeight; canvas.style.background="yellow" canvas.wid ...

Troubleshooting: The issue with json_encode in Ajax calls

I am facing an issue with my ajax call and the json response. The console is indicating that my php file is not returning a json format, but I am unable to pinpoint the exact reason behind it. Below is my ajax function: function showEspece(espece, categori ...

The challenge of maintaining consistency in Vue 3 when it comes to communication between

Context In my Vue 3 application, there is a HomeView component that contains the following template structure: <InputsComponent></InputsComponent> <CheckboxesComponent></CheckboxesComponent> <Toolbar></Toolbar> T ...

Place the div's scrollbar at the beginning of its content

Recently, I put together a custom CSS modal that includes a scrollable div (without the modal itself being scrollable). Interestingly enough, when I initially open the modal, the scrollbar of the div starts at the top as anticipated. However, if I scroll d ...

Discover the most frequent value in an array by utilizing JavaScript

My array contains repeating values: [0, 1, 6, 0, 1, 0] How can I efficiently determine the highest frequency of a specific value being repeated? For example, in this array, I would like the script to return 3 since the number 0 repeats most frequently a ...

Utilize Javascript to establish a fresh attribute by analyzing other attributes contained in an array within the object

Currently, I am working on a data structure that looks like this: masterObject: { Steps: [ { step: { required: false, }, step: { required: false, }, step: { required: false, }, }, ] } ...

When hovering over certain transitioning elements in a D3JS chart, the animation execution is paused if other elements are also in the process of transitioning

Currently, I have been designing a horizontal bar chart and experimenting with transitions on various elements like rect and circle. The transitions are applied to attributes like width and r to achieve the desired effect. Everything seems to be working fi ...

Swapping values between HTML tables and arrays with the power of JavaScript

I have a unique table structure that I need help with: https://i.sstatic.net/fr7oJ.png My current table has 2 rows and multiple columns, but I want to change it to have 2 columns and multiple rows like this: https://i.sstatic.net/uhkp9.png To create th ...

When touch-triggered on IOS, HTML5 Web SQL Transactions were smoothly bypassed without any errors

I'm encountering issues with database transactions on IOS devices. When the user does not interact with the phone, everything functions as expected. However, if the user taps, scrolls, or touches the screen, some transactions bypass the actual transac ...

Error message encountered in Express.js when trying to apply the ejs function: "View is not a constructor"

I am attempting to execute certain tasks before the original app.get function is called. After referring to this page, I applied their method which worked for the most part, except when using a rendering engine. The code snippet below demonstrates what I ...

Tips for clearing out outdated information from a bar chart

My bar chart is receiving JSON data based on the selected dropdown value. The chart updates when the dropdown changes, but there seems to be a problem with the hover functionality causing the last visited value to shake the chart. Any suggestions on how ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

Separate your HTML code and move it to a different HTML document within Angular CLI

Is there a way to extract my HTML section from the file app.compontent.ts and place it in a separate HTML document? I've tried adding the HTML code directly into the generated class app.component.ts but it doesn't seem to work. I'd also lik ...