Using Angular's ui-router to pass and access parameters in your

I am currently facing an issue while using Angular with ui-router. Everything is working smoothly except for one particular scenario. I have designed a directive element that serves as the header of the page. This element consists of a menu with 4 links. The visibility and functionality of these links are dependent on the id of the current user, which is passed as an attribute when I invoke my directive.

ui.js

angular.module('app.ui', 
[

]).directive('userHeader', function()
{
    return {
        restrict : 'E',
        templateUrl : '/scripts/directives/ui/userHeader.html',
        controller : function($scope)
        {

        },
        link : function(scope, element, attributes)
        {
            scope.uid = attributes.uid
        }
    };
});

userHeader.html

<md-list layout="row" class="md-whiteframe-z1 no-padding">
    <md-list-item class="has-link" layout-align="center center">
        <md-item-content md-ink-ripple layout="row" layout-align="center center" ui-sref="userInformations({ id : '{{ uid }}' })">
            <div class="inset">
                <md-icon md-font-set="material-icons">person</md-icon>
            </div>
            <md-tooltip hide-gt-sm>Informations</md-tooltip>
            <div class="inset" hide-sm>Informations</div>
        </md-item-content>
    </md-list-item>
    <md-list-item class="has-link" layout-align="center center">
        <md-item-content md-ink-ripple layout="row" layout-align="center center" ui-sref="userEvents({ id : '{{ uid }}' })">
            <div class="inset">
                <md-icon md-font-set="material-icons">event</md-icon>
            </div>
            <md-tooltip hide-gt-sm>Événements</md-tooltip>
            <div class="inset" hide-sm>Événements</div>
        </md-item-content>
    </md-list-item>
    <md-list-item class="has-link" layout-align="center center">
        <md-item-content md-ink-ripple layout="row" layout-align="center center" ui-sref="userGalleries({ id : '{{ uid }}' })">
            <div class="inset">
                <md-icon md-font-set="material-icons">photo</md-icon>
            </div>
            <md-tooltip hide-gt-sm>Galeries</md-tooltip>
            <div class="inset" hide-sm>Galeries</div>
        </md-item-content>
    </md-list-item>
    <md-list-item class="has-link" layout-align="center center">
        <md-item-content md-ink-ripple layout="row" layout-align="center center" ui-sref="userFriends({ id : '{{ uid }}' })">
            <div class="inset">
                <md-icon md-font-set="material-icons">group</md-icon>
            </div>
            <md-tooltip hide-gt-sm>Amis</md-tooltip>
            <div class="inset" hide-sm>Amis</div>
        </md-item-content>
    </md-list-item>
</md-list>

user.html

<user-header user-id="UserInformations.user.id"></user-header>

I am seeking guidance on how to properly bind the user-id attribute to all ui-sref attributes within my md-item-content so that the correct links can be generated.

Any assistance provided in resolving this matter would be greatly appreciated.

Answer №1

If the directive is not receiving any data, it could be causing your issue

Try updating your code like this

<user-header user-id="{{UserInfo.user.id}}"></user-header>

link : function(scope, element, attributes)
{
    scope.uid = attributes.userId;

}

For more information, check out https://docs.angularjs.org/guide/directive on how to properly pass data to a directive's scope.

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

Error on AngularJS: Module 'ngExcel' is unavailable

I encountered a peculiar problem with two computers that are both running Ubuntu 16 and have the same code pulled from GitHub. The issue arose when a new dependency, ngCsv, was added to the code. Strangely, only one of the machines started displaying the f ...

The callback function within the Service does not execute just once when utilizing $timeout

Looking to implement a service that functions similarly to this example. Here is the code I have so far: app.service('Poller', function ($q, $http, $timeout) { var notification = {}; notification.poll = function (callback, error) { return $ ...

Unexpected behavior with Angular directives

Looking for a way to achieve two-way binding in AngularJS directives with boolean values? Here's an example of a directive: var xmlToHtml = function () { return { restrict: "A", templateUrl: 'Components/XML2Html/views/XML2Htm ...

Tips for utilizing sessions to store data in Angular, Node.js, and Socket.io

I am facing a challenge when trying to save data using sessions in my angularjs, nodejs, and socket.io website. I need the session data to be cleared when the user closes the page. For instance, I have a username value stored in the express function: app ...

"Fill the designated area on the webpage with data retrieved from an external script

In my current project, I am utilizing Angular and JQuery to enhance re-usability by setting the header and footer as partials. However, I encountered an issue where I needed certain javascript functions to be executed within the footer upon loading - a tas ...

Is it better to have Angular and Laravel as separate applications or should Laravel serve the Angular app?

I have a new project in the works, aiming to create a large SAAS application. Although I come from a CakePHP background, I've decided to use Laravel and Angular for this venture. As I navigate through unfamiliar territory with these technologies, I a ...

Sending basic HTML from Express.jsSending simple HTML content from an Express.js

Within my index.html document, I have the following: <input name='qwe'> {{qwe}} I am looking to send {{qwe}} in its literal form, without it being replaced by server-populated variables. How can I achieve this? My initial thought was to ...

Searching for values using keys in Angular

Currently, I am working on a project using Angular where I need to store information based on specific identifiers. To display this information in the Angular application, I am pulling data for different identifiers and showing it on the screen. At the mo ...

Angular JS can customize the default HTML page on a select tag, offering users a

When the web page is loaded, I want to display the default tableView.html from the views folder. However, it is not working as expected. Interestingly, when I switch between "Table View" and "List View" in the dropdown menu, it works fine. I believe the ...

Interacting with AngularJS $http.post requests is significantly faster when a periodic $rootScope.$digest is in place

While navigating through our incredibly large and intricate AngularJS application, I stumbled upon a discovery that caught me by surprise. application.run(function($rootScope) { window.setInterval( () => { $rootScope.$digest(); }, 1000); }); Inter ...

Tips for retrieving the content of an input field using Angular

function HomeController($scope, navbarService) { var vm = this; $scope.showHeader = true; $scope.userNameNav =''; $scope.$on('toggleHeader', function(event, data) { $scope.showHeader = data; }); $s ...

Having trouble executing multiple file uploads with node.js resulting in an ERR_EMPTY_RESPONSE?

Currently, I am attempting to upload multiple files on FTP using node.js. Everything seems to be going smoothly as the files are successfully uploaded to the server location. However, after some time passes, I do not receive a success message. Instead, I e ...

Ways to import JavaScript into child HTMLs embedded within ng-view

I'm diving into angular for the first time and I need to figure out how to incorporate JavaScript into my HTML within ng-view. Some suggestions I came across mention adding jQuery. Can someone provide some guidance on specifically what needs to be ad ...

When using @Html.ActionLink in ASP.NET MVC-5, the URL in the address bar may change, but the page does not redirect to the destination

I am experiencing an issue with my MVC view where different Angular views are displayed based on a condition. However, once the Angular views are loaded, none of the links on the MVC page seem to be working. When I click on a link in the MVC view, the addr ...

Is it possible to build an AngularJS application without utilizing HTML5?

Having created an AngularJS application, I have noticed that it functions smoothly on modern browsers. However, some of my devices run on old browsers that do not support html5. Unfortunately, I am unable to upgrade these browsers. Is there a method to ope ...

How can I use Angular to automatically check a checkbox while a page is loading?

I have a list of checkboxes, and based on the selected checkbox, offers are displayed. Everything is working fine as expected. Below is the code snippet I have implemented: function Test1Controller($scope) { var serverData = ["Samsung Galaxy Note ...

What is the correct way to implement ng-repeat with this JSON object?

I stumbled upon a "ng-repeat test fiddle" while browsing Google, and decided to update it by adding double quotes similar to the format of my json string: http://jsfiddle.net/tRxzr/602/ However, the version I created doesn't seem to be functioning pr ...

Leveraging AngularJS for a Windows store app

After attempting to integrate AngularJS into my Windows store application, I came across a few recommended solutions: Unfortunately, these solutions did not work as expected. While I didn't encounter the Unable to add dynamic content error, AngularJS ...

What is the best way to insert images into a database?

I am currently working on an internship project that requires me to include an image when adding an employee to my table. https://i.stack.imgur.com/xif58.png https://i.stack.imgur.com/wDkY7.png We are using AngularJS on the front end and ASP.NET Core 3. ...

What is the best practice for storing angular partials - should they be kept in the public/ directory, views/ directory, or another location altogether

Currently developing a Pinterest-inspired platform to enhance my understanding of node.js. I'm contemplating where to store my partial views, such as those for rendering pins - should they be placed in the public/partials folder or within the views/ d ...