angular refreshes the controller upon directive usage

My user interface route includes different states:

        .state('projects', {
            abstract: true,
            url: "/?username&token",
            templateUrl: "projects/views/Projects.html",
            controller: 'ProjectController'
        })
       .state('projects.mine', {
            url: "mine",
            templateUrl: 'projects/views/ProjectsMine.html'
        })
        .state('projects.all', {
            url: "all",
            templateUrl: 'projects/views/ProjectsAll.html'
        })

I created a directive:

    app.directive('description', function(){
        return{
            restrict: 'E',
            templateUrl : 'projects/views/directives/ProjectDescription.html',
            controller:ProjectController
        }
    });

I have two files with the same content for the state templates, but one uses the description directive while the other directly uses the template content. However, there is an issue where the controller reloads when not using the directive.

Everything is functioning correctly, except that when I navigate to the 'all' route, the ProjectController gets reloaded. The directive consists of:

<table>
  <tr >
      <td><b>name</b> </td>
      <td>{{project.name}}</td>
  </tr>
  <tr >
      <td><b>owner</b> </td>
      <td>{{project.owner}}</td>
  </tr>
<table>

The directive is added within an ng-repeat (in the 'all' state):

 <div ng-repeat="project in project|filter:searchText">
     <description></description>
 </div>

Links to states on my HTML page:

    <a ui-sref=".mine()"><button>my projects</button></a>
    <a ui-sref=".all()"><button>all projects</button></a>

    <div ui-view></div>

The 'mine' state uses the directive content and does not trigger a controller reload. However, going from 'mine' to 'all' results in a controller reload.

Is there a way to prevent the controller from reloading while keeping everything else unchanged?

Answer №1

After removing the following code snippet:

 controller:ProjectController 

The issue was resolved. Using `controller:'controllerName'` results in a new controller being instantiated for each directive that is created.

Answer №2

Connecting to the path is similar to invoking a function:


    <a ui-sref=".list()" class="btn btn-primary">List</a>
    <a ui-sref=".paragraph()" class="btn btn-danger">Paragraph</a>

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

RecoilRoot from RecoilJS cannot be accessed within a ThreeJS Canvas

Looking for some guidance here. I'm relatively new to RecoilJS so if I'm overlooking something obvious, please point it out. I'm currently working on managing the state of 3D objects in a scene using RecoilJS Atoms. I have an atom set up to ...

Leveraging configuration files for HTML pages without any server-side code

I am currently working on developing an app using phonegap. At the moment, I have the reference to a script in every page like this: <script language="javascript" src="http://someServer/js/myscript.js"></script> If the server 'someServer ...

Navigating Through a Route - Using Node.js

What is the most effective way to execute a GET request in a route? api.js api.route('/guests') .get(function(req, res) { Guest.find(function(err, guests) { if (err) res.send(err); res.json(guests); }); ...

Using Javascript to generate a fresh object based on another object and then extracting it from an array

I have this sample constructor that creates new Loot objects: function Loot(type, sellValue) { this.type = type; this.sellValue = sellValue; } I want to extend these properties to other objects and add them to an array like so: var inventory = ...

How to utilize regular expressions in JavaScript without the need to instantiate a regex object

Here is a code snippet that checks a URL for a specific pattern: var url = "https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=regex%20javascript"; var patt = new RegExp('https?:\/\/[^/]+\.google ...

The media query appears to be ineffective on the footer section when accessed from a mobile device, yet it functions properly when adjusting the browser's screen size

I'm facing an issue with the footer section of my website. It appears very responsive when viewed from a desktop or tablet, but when I open the site on my mobile phone, the footer looks unstyled. Here is how it appears on desktop at 360px: enter ima ...

What is the method used by ng-controller to find the correct controller file?

I'm completely new to AngularJS. In my Visual Studio solution, there are multiple projects, and I want to transfer a view that uses a controller from one project to a new project within the same solution. However, I'm unsure if I need to move the ...

Transfer the html div element to the ajax request

My server-side code runs multiple bigquery queries and organizes the results in a table. The client calls this code via an ajax request. I want to send the table/div generated by the server-side code to the client so that it can be rendered there. Is this ...

Developing a Fresh Settings Tab and Vue Module in Laravel Spark

When working with Laravel Spark, you'll find that most settings panels come with a corresponding Vue JS component that is called upon using a custom tab. <spark-create-tests :user="user" inline-template> <div> </div> </sp ...

Adding ng-click to @Html.TextBoxFor in MVC Razor is a common requirement for developers working with Angular

Is it possible to add an ng-click attribute to my TextBox while using the Html.TextBoxFor method? @Html.TextBoxFor(model => model.User.Firstname, new { @class = "form-control required", placeholder = "Enter ...

Submit both file data and form data

I am encountering an issue where I can only pass either my form data or data from an uploaded file, but not both simultaneously. Here is the JavaScript code I am using to retrieve the data: var form = document.getElementById('AddDocs'); var dat ...

Encountering the error message "Unable to access property 'addEventListener' of null while trying to manipulate innerHTML"

Currently, I am utilizing innerHTML to include certain elements and a wrapper around them. for (i = 0; i < arr.length; i++) { b.innerHTML += "<div class='wrapper'><div class='col-4'>" + arr[i].storeID + "</div> ...

Use the @PostMapping annotation and remain on the current page without being redirected

I'm currently utilizing Spring Boot for my project. I have a page that displays all ads, and I am in need of adding ads to favorites without redirecting or reloading the page. Essentially, I want users to be able to click on a button to add an ad to t ...

Mishap with ShareThis.com Social Media Buttons Hit Counter

Having some issues with the hitcount on my social media share buttons. Whenever I "Share" the page on Facebook, it gets counted as a "Like" when I refresh the page. <span class='st_facebook_hcount' st_title='$videoTitle' displayText ...

Having trouble rendering a dynamic table with JavaScript utilizing a JSON object

I am struggling to retrieve data in JSON format and display it in a table. Despite trying various methods, I have been unable to make it work. Below is the code for this function. Can someone please assist me in identifying what is wrong with it? As of now ...

Swap out the current partial for a different one using AngularJS's UI router

Within the customer's interface, I have implemented a button to create a new customer which should trigger the loading of a partial view called customers.create.html, replacing the current customers.html view. Is there a way to seamlessly swap out th ...

What is the best way to perform an atomic update on an embedded document within an array using mongoose? The operation should only update the latest document and then return the updated

Currently, I am delving into node.js and MongoDB with mongoose. //Document in mongoDB { name: 'first', _id: '1000' phases: [{ _id: 1, phaseName: 'phase1', stories: [{ _id: s1, sname: ...

Is it possible to generate a form dynamically in a Vue.js HTML file based on JSON data? Check out the provided f

Currently, I am diving into the world of vue.js and experimenting with building a dynamically created form. To explain further, I have JSON files that define what input types should be included in the form; now I am figuring out how to implement this usin ...

"Seeking clarification on submitting forms using JQuery - a straightforward query

My goal is to trigger a form submission when the page reloads. Here's what I have so far: $('form').submit(function() { $(window).unbind("beforeunload"); }); $(window).bind("beforeunload", function() { $('#disconnectform&apo ...

Triggering special characters in jQuery autocomplete

I am facing a specific issue: I need to create a customized autocomplete feature using jQuery that is triggered by the "@" character. The problem arises when I start typing in the textbox with "@", it works fine. However, if I enter "@" after typing some ...