Adjust the color of the active link on the page using Angular and CSS

I have a project that I need to modify by adding a sub menu that appears on every page but is only coded once. My goal is to highlight the link for the current page, all within one HTML snippet. Although the list renders correctly, I'm struggling to get the current page to highlight. While I've seen suggestions about adding 'current link' formatting in a separate file, I must include all the code in this snippet. This task requires the use of HTML, CSS, and Angular, and I am new to learning Angular.

Given these constraints, is it possible to achieve what I am trying to do? If so, how can I make it work?

body {
  font-family: Source Sans Pro;
}

ul {
  float: left;
  width: 100%;
  padding: 0;
  margin: 0;
  list-style-type: none;
}

a {
  float: left;
  width: 6em;
  text-decoration: none;
  padding: 0.2em 0.6em;
  border-right: 1px solid white;

}

#navbar li a.current {
  background-color: #FFF;
}

li {
  display: inline;
}

#bl {
  background-color: #5a5a5a;
  color: yellow;
}

#or {
  background-color: #5a5a5a;
  color: yellow;
}

#gr {
  background-color: #5a5a5a;
  color: yellow;
}

#bl:hover, #or:hover, #gr:hover {
  background-color: #ff6900;
}

.left {
  float: left;
}
 
<body ng-app="watch"> 
  <div ng-controller="control"> 
    <ul>
      <li><a href="#" ng-click = "person()" id="bl">Person</a></li>
      <li><a href="#" ng-click = "product()" id="or">Product</a></li>
      <li><a href="#" ng-click = "place()" id="gr">Place</a></li>
    </ul>
  </div>
</body>
my controller look like this

'use strict';
var mymodule = angular.module("watch", []);

mymodule.controller("control",function($scope) {
    $scope.jsonerator = true;
    $scope.jsonerator2 = false;
$scope.jsonerator3 = false;


    $scope.person = function() {
        $scope.jsonerator = true;
        $scope.jsonerator2 = false;
$scope.jsonerator3 = false;

 }


 $scope.product = function() {
        $scope.jsonerator = false;
        $scope.jsonerator2 = true;
$scope.jsonerator3 = false;

}


 $scope.place = function() {
        $scope.jsonerator = false;
        $scope.jsonerator2 = false;
$scope.jsonerator3 = true;
   }
};

and this is my menu on jsfiddle:jsfiddle

Thanks

Answer №1

If you're looking to streamline your code, consider transitioning from CSS IDs to classes and possibly removing IDs from your anchor tags. Another approach could be utilizing ng-class; it automatically adds a class based on a boolean condition. In this scenario, we are referencing the variables in your jsonerator $scope.

<li><a href="#" ng-click = "person()" id="bl" ng-class="{bl: jsonerator}">Person</a></li>
<li><a href="#" ng-click = "product()" id="or" ng-class="{or: jsonerator2}">Product</a></li>
<li><a href="#" ng-click = "place()" id="gr" ng-class="{gr: jsonerator3}">Place</a></li>

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

Fancybox causing issue with submit button functionality

When a link is clicked, I am triggering the opening of a submit button styled fancybox. The fancy box contains fields for username and password that need to be authenticated upon clicking the submit button (for now, the fancybox is just a submit button). ...

I am dealing with a set of divs wrapped in a flex container that tend to overlap each other. Their heights vary and sometimes they wrap multiple times. Any suggestions on how to prevent this

I am encountering an issue where the width of the div remains the same after it has wrapped, causing overlapping. I want the first div to push the second to the right and so on. When I add a 100% width, it makes the width for the smallest div the same as ...

What could be causing the appearance of sha256.js when making an AJAX request to a PHP script using jQuery?

Currently, I am executing a script that saves modifications to a PHP script in the background using jquery-ajax. Additionally, I have implemented a function that triggers an error if the script attempts to post something on the site. In case of an error, I ...

Leverage HTML within JSON for a multilingual website using i18next with Next.js

I am working on a multi-language website using Next.js. To handle the translations, I am using the package i18next. In my JSX code, I define variables like this: {t("satisfied:title")} This means {t("JSONfileName:JSONvariable")} However, when I try to i ...

Creating a dynamic full-width background image that adjusts its height according to the content: a step-by-step guide

Currently, I'm attempting to apply a background image to a webpage that spans 100% width in order to adjust to the viewport size. The method I am using involves placing an image directly after the body tag with the subsequent styling: img#background ...

Sharing functions between Angular components

Check out my problem statement: https://stackblitz.com/edit/angular-jk8dsj I'm facing two challenges with this assignment: I need to dynamically add elements in the app.component when clicking a button in the key-value.component. I've tried ...

Accurate date and time depiction at a high resolution using JSON and JavaScript

Is there a standardized method for displaying high-resolution timestamps in JSON and/or JavaScript? It would be ideal to have support for at least 100 ns resolution, as it would simplify the server code (due to the 100 ns resolution of the .NET ...

The color of the Angular md-switch .md-thumb is not showing up properly when switching the toggle

In Safari, the md-switch displays a yellow section on the md-thumb when toggled. However, other browsers handle the switch without any issues. The md-bar appears fine, but the md-thumb is yellow. I have tried setting everything to the blue color I am using ...

Setting environmental variables throughout your application using create-react-app on the front end

Hey there! I have a simple Todo app with the client using create-react-app and the server running on node. I'm struggling to properly set environment variables across my app. I've placed my .env file in the root directory of the app, which curre ...

In what way can a programmer create HTML tailored to a designer's needs, even without a comprehensive grasp of

Currently, I am diving into my most ambitious website project yet. It's worth mentioning that I'm utilizing ASP.NET MVC 2 and the Microsoft stack. I value design and aesthetics greatly, knowing they can make or break the success of this endeavor ...

Tips on locating information within a pre-existing GET array with parameters provided

Apologies for the unclear title. I am currently utilizing a category chooser that pulls categories from an API. The process involves fetching a list of categories, filtering out their names, and presenting them in the category chooser. Upon clicking submit ...

Unable to make changes in ng-grid for numeric type data

I've set up a ng-grid, but the edit feature isn't functioning correctly. The design includes a column with numeric input type: $scope.cellInputNumericTemplate = '<input type="number" ng-input="COL_FIELD" ng- model="COL_FIELD" ng-class= ...

Challenges with character encoding in NextJS

I am currently dealing with a line of code that creates a dynamic route. <Link href={`/bundeslander/${urlName}`} ><div className=" text-blue-400">{state.name}</div></Link> The variable urlName is generated by fetching dat ...

show a notification once the maximum number of checkboxes has been selected

I came across this code snippet from a previous question and I'm interested in making some modifications to it so that a message can be displayed after the limit is reached. Would adding a slideToggle to the .checkboxmsg within the function be the mos ...

Avoiding the insertion of styles into the HEAD section when using Webpack MiniCssExtractPlugin in combination with Create React

Currently, I am utilizing create-react-app to develop a component library with Storybook JS. The ultimate goal is to release an NPM package containing these components for use in various projects. Within this library, SASS is employed, complete with global ...

When `rxjs` repeat and async pipe are applied and then removed from the DOM, the resulting value becomes null

One of the classes I have is responsible for managing a timer. It contains an observable that looks like this: merge( this._start$, this._pause$ ) .pipe( switchMap(val => (val ? interval(1000) : EMPTY)), map( ...

Displaying the product quantity counter in the shopping cart immediately after adding the item

My website has a shopping cart quantity counter that doesn't update immediately when a product is added. Instead, it requires a page reload or navigation to another page for the change to be reflected. I would like the quantity counter to show the pro ...

Combine lists with floating elements positioned to the left or right

My HTML includes the following ul tag: <ul style="position:absolute"> <li>list 1</li> <li>list 2</li> <li>list 3</li> <li> list 4 <p>list 4 content goes here</p> ...

Charts created using Google VisualizationORThe visual representations

My chart is currently not displaying at 100% width as I intended. I would like the chart to span from the beginning to the end of the container. https://i.stack.imgur.com/Xjw6g.png Here's my code snippet: test.controller('testCtrl', [&apo ...

Javascript Object and Conditional Statement

I am working on a Dygraph object creation script that includes a data variable. However, I want to avoid passing in this variable for older browsers. Here is the code snippet: function prime() { g = new Dygraph( document.getElementById("g"), d ...