The battle between ui-sref-active and $state.includes() is a topic of

I am attempting to highlight the clicked area by adding an active class using ui-sref-active. Unfortunately, it is not working with $state.includes(), even though I can see its value as true in the controller. Can someone assist me with this issue?

See the code snippet below:

var app = angular.module('myApp', ['ui.router']);
app.controller('myCtrl', function($scope,$state) {
$scope.res=$state.includes('')
});
app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('page');
    $stateProvider
      .state('page', {
        url: '/page',
        template: "You clicked on Luffy"
      })
      .state('paper', {
        url: '/paper',
        template: "You clicked on Zoro"
      })

  })
.active {
      background-color: red;
      color: white;
    }
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>

<body>
  <div ng-app="myApp" ng-controller="myCtrl">
    <hr>
    <h2>with ui-sref-active</h2>
    <button ui-sref-active=active ui-sref='page'>Luffy</button>
    <button ui-sref-active=active ui-sref='paper'>Zoro</button>
    <hr>
    <h2>with {'active':$state.includes('')}</h2>
    <button ng-class="{'active':$state.includes('page')}" ui-sref='page'>Luffy</button>
    <button ng-class="{'active':$state.includes('paper')}" ui-sref='paper'>Zoro</button>
    <hr> Result=
    <ui-view></ui-view>
    <hr>
  </div>
</body>

</html>

Answer №1

In HTML, you cannot directly access $state, but you can invoke a controller function to accomplish this.

var app = angular.module('myApp', ['ui.router']);
app.controller('myCtrl', function($scope,$state) {
  $scope.res = $state.includes('');
  $scope.checkActive = function(state) {
    return $state.includes(state)
  }
});
app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('page');
    $stateProvider
      .state('page', {
        url: '/page',
        template: "u clicked on Luffy"
      })
      .state('paper', {
        url: '/paper',
        template: "u clicked on Zoro"
      })

  })
.active {
      background-color: red;
      color: white;
    }
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>

<body>
  <div ng-app="myApp" ng-controller="myCtrl">
    <hr>
    <h2>with ui-sref-active</h2>
    <button ui-sref-active=active ui-sref='page'>Luffy</button>
    <button ui-sref-active=active ui-sref='paper'>Zoro</button>
    <hr>
    <h2>with {'active':$state.includes('')}</h2>
    <button ng-class="{'active':checkActive('page')}" ui-sref='page'>Luffy</button>
    <button ng-class="{'active':checkActive('paper')}" ui-sref='paper'>Zoro</button>
    <hr> Result=
    <ui-view></ui-view>
    <hr>
  </div>
</body>

</html>

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

Checkbox click event not triggering properly

I am facing challenges in triggering an onclick event for the "Elevation" checkboxes located at the URL provided above. <input type="checkbox" value="A" id="elevation_A" onclick="changeElevation(this.value);" /> For some reason, the "changeElevati ...

Why does Javascript Tic-Tac-Toe delay notifying the user until the entire board is full?

There have been a lot of questions about Tic-Tac-Toe javascript, but my issue is unique. My code works fine, but it doesn't declare a winner or a tie game until the entire board is filled. I suspect that my if statements are causing problems and can&a ...

Omit specific module from a webpack 4 chunk

Is there a way to exclude a specific module from being included in a chunk using webpack 4? For example, let's say I do not want lodash to be included in the vendor file at all costs. How can this be achieved? Here is the current configuration: spli ...

Is Protractor compatible with Internet Explorer 9?

For my Angular App that is running on IE9, I need to create end-to-end acceptance tests. I'm curious to know if the browser simulated by Protractor matches the behavior of IE9 or a newer version? ...

Transmitting a client-side JavaScript function to the server for processing the database response and generating a downloadable CSV file

I need to create CSV reports for a series of queries in my project. In the backend, I have a general POST request handler in nodejs/express that accepts a JSON object structured like this: { "converter": <converter function>, "fields": < ...

Issue with Context Menu Not Triggering on Dynamically Added Elements in JQuery

Check out the JSFiddle Demo Within my email sidebar, I implemented a custom right-click feature that allows users to add new sub-folders. The code snippet below demonstrates how this functionality works: if ($(this).hasClass('NewSubFolder')) { ...

How can we initiate an AJAX request in React when a button is clicked?

I'm fairly new to React and I'm experimenting with making an AJAX call triggered by a specific button click. This is how I am currently using the XMLHttpRequest method: getAssessment() { const data = this.data //some request data here co ...

Troubleshoot issues within ExpressJS

I am encountering an issue with the debugger in Visual Studio Code. It crashes upon startup, but runs smoothly when using nodemon to start the server. My application is connected to a MySql Database. I have attempted to reinstall the module without succes ...

An element in CSS that has a position of fixed and a width of 100% surpasses the dimensions of its

My element has the CSS properties position:fixed and width:100%, causing it to be larger than its parent elements. Despite the complexity of my code, I have attempted to simplify it for better understanding. Below, you can see that the green box exceeds ...

Tips for utilizing process.stdin in Node.js with Javascript?

Currently, I'm developing a Javascript-based poker game that is designed to process standard input in the following manner: https://i.stack.imgur.com/D1u15.png The initial line of input will consist of an integer indicating the total number of playe ...

Exploring JSON data structures using autocomplete functionalities

Here's the code I'm working with: <s:hidden id="s" value="%{Users}"/> The variable Users contains an array list of User objects. This code is written in Javascript. I want to access Users as JSON for auto-complete functionality: var valu ...

Having trouble exporting a variable from one Node.js file to another and finding that the value remains unchanged?

Hey there, I've been working on exporting a variable to another file within my nodejs application. I have successfully exported the variable, however, I need it to update whenever a user logs in. Will the export automatically pick up on this change an ...

Adjustable Bootstrap Progress Bar - Modify element width on the fly

I have encountered an issue with my progress bars on the webpage. The static HTML version works perfectly fine, but the dynamically created one using jQuery seems to be instantly at 100% without any delay in progression. To illustrate the problem better, ...

Utilize AJAX response to mark checkbox as checked

I have an HTML checkbox that I am attempting to check using a script received as an ajax response. Below is my HTML snippet: <form class="form-vertical sms-settings-form"> <div class="form-group"> <div data-toggle="tooltip" titl ...

Allow JavaScript to determine whether to link to an internal or external web address

Currently, I am in the process of setting up a new website and need to create an HTML page with some JavaScript that can determine whether to link to the external or internal IP address. I have researched some JavaScript code to fetch the IP address, whic ...

Jquery spinner overlay for enhanced loading experience

Currently, I am incorporating jQuery/Ajax in my project and wanted to include a spinner when an ajax request is made. The specific scenario involves two tabs on the UI: Home and Activities. When the user clicks on the Activities tab, an ajax request is sen ...

Having trouble storing radio buttons and checkboxes in MySQL database using AJAX

My app is facing an issue where radio buttons and checkboxes are not correctly entering information into the database. Currently, only text fields are successfully saving data, while checkboxes and radio buttons are only recording the value of the first ra ...

Enhancing React Functionality: Increasing React State Following an If Statement

Whenever I click on the start/stop button, it triggers the handlePlay function. This function then proceeds to initiate the playBeat function. In an ideal scenario, the play beat function should continuously display 1222122212221222... until I press the st ...

Locate a specific option that matches the value of the selected data-status and set it as "selected" using jQuery

Currently, I am facing an issue where I need to load 2 separate ajax responses into a select dropdown. The select dropdown will have a data-status attribute, and my goal is to loop through the options to find the one that matches the value of the select da ...

Utilize a single WebAssembly instance within two separate Web Workers

After compiling a wasm file from golang (version 1.3.5), I noticed that certain functions using goroutines are not supported. When these functions are called, they run in the current thread and slow down my worker significantly. To address this issue, I w ...