What causes my ready function to consistently execute upon controller or directive reinitialization?

Every time my controller or directive reinisilize, the

angular.element(document).ready(function(){...});
function is executed. Why does this happen?

In my scenario, I have two controllers and one directive. I update a value in the parent controller which is then assigned to an ng-repeat directive. Whenever this variable changes, the controller reinitializes causing the ready function to be recalled as well.

To view an example, please visit: angualr ready function

Answer №1

Your current code structure involves the destruction and recreation of elements upon clicking the "change index" button:

that.changeIndex = function(){
    that.arr = [{name : g++}];
    that.isValid = !that.isValid;
}

ng-repeat initiates the destruction and recreation of your myCon2 controllers when creating a new arr array:

<div ng-repeat="m in con.arr track by m.name" ng-controller="myCon2 as con1">

Similarly, ng-if triggers the destruction or recreation of your my-dir directive upon changing the value of isValid:

<div my-dir ng-if="con.isValid">hey directive!!!!</div>

Both the controller and directive are set to execute a handler function upon document ready, ensuring it runs smoothly even if the code loads after the initial readiness state. Although using document ready is common practice, it's not necessary within Angular controllers and directives. Initialization code can be placed at the beginning of these components or utilize ng-init.

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

Troubleshooting: Resolving the "npm ERR! missing script: start" Issue

I'm encountering the error below: npm ERR! missing script: start npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\..\AppData\Roaming\npm-cache\_logs\2019-04-27T18_02_39_6 60Z-debug.log Th ...

Applying a custom class to a particular row in an AngularJS datatable using the created

I am currently working with AngularJS datatables and I want to apply a custom class to a row if a specific condition is met. Here's what I have attempted so far: this.dtOptions = DTOptionsBuilder.newOptions() ... .withOption('createdRow', f ...

What role does the sequence play in matrix transitions that involve rotating and translating simultaneously?

Attempting to animate a matrix3d with both rotation and translation concurrently has yielded unexpected results for me. It seems that changing the order of applying rotation and translation produces vastly different outcomes. http://jsfiddle.net/wetlip/2n ...

Is it possible to inject $scope into a child model?

In the process of developing a fairly complex AngularJS application, I have realized that my primary controller model should be made up of multiple self-contained objects that will be utilized by other controllers. While this setup presents no major issues ...

Pre-selected default value in select box using ng-options

I'm working on setting a default value in my select box, retrieved from the database, using ng-options. Here's my view: <select class="form-control samlength modalinput" ng-options="p.procid as p.procname for p in proce ...

Exploring the power of pagination using Django and ExtJS

Extjs offers a gridpanel with pagination functionality, but I believe that the pagination only works once all the data has been received from the server (please correct me if I am mistaken). In my situation, the total amount of data from the server is 20MB ...

Arrange the columns of the table in ascending or descending order

I am currently working on a table that has the functionality to sort columns in ascending/descending order when clicked. However, I want each column to retain its previous sorting state and switch to the opposite order when clicked again. For example, if I ...

Populating a HTML document with data retrieved from an AJAX request

I have a button that, when clicked, should display the values of a specific user. The code snippet for this functionality is as follows: HTML Button: <button onclick="return getUserDetails(<? echo $user['id']; ?>)" class="btn btn-xs bt ...

Displaying a date picker within a fragment nested inside another fragment

Encountering a slight issue with implementing a Date picker using a fragment into another fragment. Upon clicking the button to load the Date picker, an error message is logged: java.lang.ClassCastException: com.example.buzzamaid.HomeActivity cannot be ca ...

Adjustable Title in Line Plot

After receiving a JSON object from a web server in response, I am encountering some challenges with extracting and displaying the data. The JSON array consists of monthly key-value pairs like {"JAN":"17"}, {"FEB":"19"}, {"MAR":"21"}, etc. To display these ...

Displaying a text in a Django template as a JSON entity

I am currently facing a challenge with an angular app that sends JSON data to a Django backend. The Django application saves the JSON data into a database and later retrieves it to send it back to the angular app. However, I am struggling to get this entir ...

Issue with Pure Javascript FormData upload involving files and data not successfully processing on PHP end

My file upload form follows the standard structure: <form id="attachform" enctype="multipart/form-data" action="/app/upload.php" method="POST" target="attachments"> <!-- MAX_FILE_SIZE must precede the file input field --> <i ...

"Repeating SignalR Messages: Issue of Duplication when Stopping and Restarting

Whenever I stop and start the connection, messages sent to the client by the hub are duplicated. If I follow this sequence: $.connection.hub.stop() $.connection.hub.start() {...} and a message is sent from the server hub to the client, it is initially re ...

The information submitted through the form is not displaying on the console after being gathered using the post method in Express

When attempting to add products using a form on the admin page, the data (including an image file) is not being displayed in the console as expected. Instead, the following message appears: "{} POST /admin/add-product - - ms - -" Below is th ...

Creating a Mongoose schema to store an array of objects, where updates will automatically add new objects

const mongoose = require('mongoose'); module.exports = mongoose.model('GridModel', { Request_Id : { type : Number, required : true }, viewStudents : { type : Array , default : [] } }); The mongoose model above needs to b ...

Having trouble transmitting data from the View to the Controller

Need help with this issue. I'm having trouble passing my data to the controller. Below is my ajax code. <script type="text/javascript"> $(document).on("click", "#login_button", function () { var userName = document.getElementById(" ...

mandating the selection of checkboxes

Currently, I am exploring the possibility of automatically selecting a checkbox when an option is chosen from a dropdown menu. Below is a code snippet that demonstrates what I am aiming to tweak: $('.stackoverflow').on('change', func ...

Struggling to understand JSON in joint.js?

I am trying to utilize the joint.js library to render a JSON data as a chart... let paper = new joint.dia.Paper({ el: $('#paper'), width: 600, height: 200, model: graph }); let graph = new joint.dia.Graph; let json = '{"em ...

Once chosen, zoom in on the map to view the results

I am facing an issue with multiple selects in my code, where one select depends on the result of another. The ultimate goal is to zoom in on the area that has been searched and found, but unfortunately, it is not functioning as expected. If you'd lik ...

Guide to establishing a connection to the Companies House API: Essential guidelines on setting up cURL headers and requisite API key specifications

I am attempting to establish a connection with the UK Companies House API, preferably using JavaScript. However, I am currently trying to set up this PHP version. How can I obtain access to the API using an API key? PHP: public function GetCompanyHouse( ...