Angular Display of Scrolling Feature not Functioning Properly

After clicking on show5 and then goto5, everything functions as expected. However, when clicking on showngo5, it does not work as anticipated, despite the fact that the same methods are being called.

What is causing showngo5 to not work in this plunker

html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <title>Example - example-example51-production</title>
  <link href="style.css" rel="stylesheet" type="text/css">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script src="script.js"></script>
</head>
<body ng-app="anchorScrollOffsetExample" ng-controller="headerCtrl">
  <div class="fixed-header">
    <a href="" ng-click="gotoAnchor(x.i)" ng-repeat="x in ids" ng-show="x.visible">
      goto{{x.i}}
    </a><br/>
    <a href="" ng-click="toggle(x)" ng-repeat="x in ids">
      <span ng-show="x.visible">hide</span><span ng-show="!x.visible">show</span>{{x.i}}
    </a><br/>
    <a href="" ng-click="showngo(x)" ng-repeat="x in ids">
      <span ng-show="x.visible">hide</span><span ng-show="!x.visible">showngo</span>{{x.i}}
    </a>
  </div>
  <div id="anchor{{x.i}}" class="anchor" ng-repeat="x in ids" ng-show="x.visible">
    Anchor {{x.i}} of 5
  </div>
</body>
</html>

js:

(function(angular) {
  'use strict';
angular.module('anchorScrollOffsetExample', [])
  .run(['$anchorScroll', function($anchorScroll) {
    $anchorScroll.yOffset = 100; // always scroll by 50 extra pixels
  }])
  .controller('headerCtrl', ['$anchorScroll', '$location', '$scope',
    function ($anchorScroll, $location, $scope) {
      $scope.ids = [
        {i:1,visible:true},
        {i:2,visible:true},
        {i:3,visible:true},
        {i:4,visible:true},
        {i:5,visible:false}
      ];
      $scope.toggle = function(x) {
        if(x.visible){
          x.visible=false;
        }else{
          x.visible=true;
        }
      };
      $scope.showngo = function(x) {
        $scope.toggle(x);
        $scope.gotoAnchor(x);
      };
      $scope.gotoAnchor = function(x) {
        var newHash = 'anchor' + x;
        if ($location.hash() !== newHash) {
          $location.hash('anchor' + x);
        } else {
          $anchorScroll();
        }
      };
    }
  ]);
})(window.angular);

css:

body {
  padding-top: 100px;
}
.anchor {
  background-color: DarkOrchid;
  margin: 2px;
  padding: 10px 10px 300px 10px;
}

.fixed-header {
  background-color: rgba(0, 0, 0, 0.2);
  height: 100px;
  position: fixed;
  top: 0; left: 0; right: 0;
}

.fixed-header > a {
  display: inline-block;
  margin: 5px 15px;
}

Answer №1

modify the code on line #25 of the JavaScript file

$scope.goToSection(x.id);

The issue arises when attempting to pass the x object to the goToSection function, which is expecting a number instead.

Answer №2

Make sure to update the showngo function with the code provided below. The issue in the current code is that when you use $scope.gotoAnchor(x); within the showngo method, it ends up scrolling to anchor[Object,Object] instead of the intended anchor5 because the entire object is being passed to x.

$scope.showngo = function(x) {
    $scope.toggle(x);
    $scope.gotoAnchor(x.i);
};

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

The JQuery pagination feature fails to function properly once an AJAX load is initiated

I am using jspages.js to implement pagination with jQuery. Everything works fine when the page is initially loaded. However, I encounter an error when the content for pagination is loaded after an ajax call. The plugin does not seem to work as expected. ...

Retrieving the checkbox's status from the database

I have implemented a basic checkbox that updates its state in the database when clicked or unclicked. Is there a way to retain this state and have it displayed as checked if the page is refreshed? Essentially, I want the checkbox to remember its last state ...

What is the best way to incorporate child nodes when selecting dynamically generated elements through JavaScript?

Currently, I have buttons being dynamically generated on the page using plain JavaScript. For example: <button class="c-config__option c-config__option--button c-config__option--pack" data-index="0"> Item 1 <span>Subtext</span> ...

Is there a way for me to extract and showcase the initial 10 items bearing a particular class name from a different html document on my homepage?

I am looking to extract a list of movies from an HTML file titled "movies.html". The structure of the file is as follows: <div class="movie">Content 1</div> <div class="movie">Content 2</div> <div class=" ...

"Guide to triggering the display of a particular div based on its class when clicked

I have multiple div elements with the class name dis HTML: <div class="dis">Content</div> <div class="dis">Content</div> <div class="dis">Content</div> and so on ... Additionally, there are various images: <img sr ...

Optimizing my AngularJS bundle is pushing me towards upgrading to Angular 5

Regarding my AngularJS application, it was initially created using 'yo angular-fullstack' with JS scripting instead of TS. It is functional but experiencing performance and user experience issues. The deployment is on AWS ElasticBeanstalk nano i ...

Retrieve element attributes and context inside a function invoked by an Angular directive

When working with a directive definition, you have access to the $element and $attrs APIs. These allow you to refer back to the element that called the directive. However, I'm curious how to access $element and $attrs when using a standard directive l ...

Retrieving a json file from a local server by utilizing angularjs $http.get functionality

After fetching a JSON file from localhost, I can see the data when I console log. However, when I inject the Factory into my controller, it returns a null object. This indicates that the variable errorMessage does not receive the JSON object because the ...

Glitch with AngularJS Bootstrap date picker

Currently, I am working on integrating a datepicker into my PhoneGap application using Angular UI Bootstrap. <h4>Popup</h4> <div class="row"> <div class="col-md-6"> <p class="input-group"> <input type="text" cl ...

What is the best method for creating an HTML table using a Javascript function in PHP?

When working with a PHP file to generate a table based on SQL query results, I encountered an issue where only one incorrect row is returned. If anyone has a basic idea of what might be happening, it would be greatly appreciated. This is my HTML Code: &l ...

What is the reason for the back button appearing next to the slide menu?

https://i.sstatic.net/t344j.pngI am currently working on a project using the Ionic framework, and I want to create an app with a slide menu. However, I do not want to display the slide menu on the first screen. Instead, I have a button on the initial scree ...

What could be causing an issue with CORS in ExpressJS in one scenario but not in another?

I am currently in the process of setting up a database and connecting it to various routes. Interestingly, I have been successful with one route ('register') but encountering issues with another ('login'). Whenever I attempt to run the ...

Problems arise when using AngularJS' .run function after navigating to a different page

I have encountered an issue with ngRoute while navigating between pages in my web application. The main login page is called index.html, and the routing is controlled by the main js file. However, I face a problem when trying to use a .run block on a speci ...

Querying MongoDB to locate books by the same author or those that are categorized in at least one similar category

Looking to discover books by the same author or with at least one matching category. This is how my Book Schema looks: const bookSchema = new Schema( { title: { type: String, required: true }, author:{ ...

How come I'm unable to send the HTTP request contents as a response using express.js?

I'm currently in the process of setting up a basic web server using node.js and express.js. Essentially, I want the server to return the content of the request as a response. Below is the code I have implemented: const express = require('express& ...

Steps for storing div content (image) on server

Currently in the process of developing a web application that allows users to crop images. The goal is for users to have the ability to email the URL so others can view the cropped image, ensuring that the URL remains active indefinitely by storing every c ...

Creating a Webgrid in MVC and integrating it with a custom class in AngularJS

Hey there, I'm a beginner when it comes to AngularJS and I'm looking to bind the webgrid within the method of AngularJS. $scope.SaveDetails = function () { debugger; var UserID = '@Session["ID"]'; ...

The Java Servlet change did not trigger an update in the web content

While using Eclipse to develop a website where Servlet sends data to JSP, I encountered an issue. Despite modifying the data in the Servlet, it continued to send outdated information to the JSP page. I attempted the following options: Menu - Project - cle ...

Vuejs Error: The 'in' operator cannot be used for searching

I am facing an issue with my form and VueJS. Upon clicking the "Login" button, I intend to change the text on the button. However, instead of achieving this, I encounter an error stating 'cannot use 'in''. Below is the HTML code snippet ...

Does creating a form render the "action" attribute insignificant in an AJAX environment?

When submitting forms exclusively through AJAX, is there any advantage to setting the action attribute at all? I have yet to come across any AJAX-form guides suggesting that it can be left out, but I fail to see the purpose of including it, so I wanted t ...