I am struggling to grasp the concept of the named controller in Angularjs and how the keyword "this" is utilized

I'm trying to grasp the concept of using named controllers with the controller as syntax in Angular. Within my application, I have assigned the same controller to different divs, always naming the controller as myCtrl as C in the HTML. Inside the controller, I make use of this to refer to its properties and methods. The controller contains variables and methods that are linked to and called from the HTML page.

I've encountered some unusual behavior that I attempted to replicate in the following example:

var app = angular.module("myApp", []);

app.controller("ctrl1", function(){
    var vm = this;
    vm.name = "Pippo";
    vm.msg = function(){alert(vm.name)}
})

app.controller("ctrl2", function(){
    this.name = "Pippo";
    this.msg = function(){alert(this.name)}
})

app.controller("ctrl3", function($scope){
    $scope.name = "Pippo";
    $scope.msg = function(){alert($scope.name)}
});

http://jsfiddle.net/k66Za/111/

In my specific scenario, I am using ctrl1. Upon clicking the first button, I expect it to return the name associated with ctrl1, however, it returns the name associated with ctrl2 instead.

I acknowledge that this discrepancy is due to my lack of understanding, but I am unable to discern the difference between ctrl1 and ctrl2. Could someone please clarify what's going on in my example and advise on whether I should use $scope, this, and why there is a distinction between using vm (where vm = this) versus using this?

Answer №1

Despite their similar names, there is no actual distinction between ctrl and ctrl2. However, the reason for choosing to follow the approach taken in ctrl rather than ctrl2 is as follows:

It is advisable to bind the controller this to a variable for future reference. The behavior of this varies depending on the context in which it is used. When referenced within a function, it pertains to that specific function. If you wish to bind something to the controller from within a function, using this is not the correct method.

When $scope is injected into a controller, it serves as a 'glue' between the controller and the view, acting as an intermediary between the two entities. By utilizing the controller as syntax, you interact directly with the controller instance, necessitating the reference to myCtrl.myVar instead of just myVar (when attached to $scope).

Your provided code snippet showcases the following:

<div ng-controller="ctrl2 as c"  ng-init="c.name=3">
        Name: <input type="text" ng-model="c.name" />
        <input type="button" ng-click="c.msg()" value="I should show {{c.name}}" ></input>
        </div>

        <div ng-controller="ctrl2 as c"  ng-init="c.name=4">
        Name: <input type="text" ng-model="c.name" />
        <input type="button" ng-click="c.msg()" value="I should show {{c.name}}" ></input>
        </div>   

Upon initial assessment, the behavior of this code may appear peculiar, but in reality, it is functioning as designed. Regardless of the button clicked, both yield the value 4. This outcome is a result of instantiating the same controller twice. During Angular's digestion of the page, it encounters the first instantiation, assigns the value 3 to c.name, and subsequently, the second instantiation assigns 4 to c.name. Therefore, any attempt to reference c.name will retrieve the latter value.

Answer №2

After some exploring, I think I might have stumbled upon a solution to the issue at hand. Let's consider the following scenario, illustrated in this example: http://jsfiddle.net/k66Za/113/ It appears to be not functioning as expected - when clicking the first button, the value returned is 2 instead of 1.

As a potential fix, I made a simple adjustment by replacing the line vm=this with var vm=this

Surprisingly, this modification seemed to have resolved the issue and the functionality now operates correctly. What could be the reason for this difference?

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

What steps can I take to address this issue with my express node and ejs?

Hey there, I'm new to node.js and I've been encountering this error message. Can someone please provide some insight? Error: Could not find matching close tag for "<%=". at /Users//Desktop/Web Development/getting_started_express js/node_m ...

Using nodeJS's util module to format and pass an array

I've been using util.format to format strings like this: util.format('My name is %s %s', ['John', 'Smith']); However, the second parameter being an array ['John', 'Smith'] is causing issues because m ...

What is the best way to maintain the previous input value on page refresh in Vuejs?

In my development project, I am utilizing the Laravel and Vue.js technologies. I am facing an issue where I need to set the input value to its previous old value when either refreshing the page or navigating back to it in a Vue dynamic component. Here is ...

Calculate sums in ReactJS without the need for a button

Adding a few numbers might seem like an easy task, but I've been unable to do so without using an explicit button. // Using useState to handle state changes const [ totalCount, setTotalCount ] = useState(0) // Function to add numbers of differ ...

Using setTimeout in Node.js

I've been struggling to find a solution for slowing down the timeout in my code. The issue is that it runs too quickly, and I can't seem to figure out how to adjust it using Request. Everything else in the code works perfectly. var Scraper = fun ...

Leverage the power of forkJoin in JavaScript by utilizing objects or sourcesObject

I'm currently facing an issue with my code snippet below: getInformations().subscribe( informations => { let subs = []; for (const information of informations) { subs.push(getOtherDetails(information.id)); } ...

Guide to adding information to a file in Nodejs depending on a condition

Looking for assistance on how to append an annotation (@Circuit(name = backendB)) to a file if the "createEvent" name exists and the annotation is not already present. I am unsure of the process, so any help on checking and appending using streams would ...

Explore Dreamfactory's User Management features for apps with varying user roles and permissions

I am currently working on an application that requires user management with various roles to access different data views stored in a MS SQL Server database. To streamline the process, I am using dreamfactory to create a REST API for this data. My goal is t ...

Transform JSON data into an HTML table display using JavaScript/JQuery

To retrieve the JSON output on the user interface, I use the following function: $("#idvar").click(function(){ var d1 = document.getElementById("dText"); var d2 = document.getElementById("dJson"); var mytext = d1.textContent; alert(mytext); $.po ...

Trouble with radio button selection in Pyppeteer, puppeteer, and Angular JS

I am struggling to select the 'fire' option in a radio button within a div element using Pyppeteer. Despite multiple attempts, I have not been successful. Here is the structure of the div with the radio button: <div _ngcontent-xqm-c396=" ...

Experiencing difficulties with executing numerous NodeJs queries

Could someone assist with rendering multiple queries in a Nodejs view? The console shows Promise {pending} and I can't figure out what's causing the issue. I'd appreciate any help or guidance on how to fix this problem. router.get('/ ...

What are some ways to lazily load directives in AngularJS?

Currently, I am exploring the capabilities of angularjs and aiming to dynamically load directives only when they are required instead of loading all of them initially on the page. My focus is on creating custom directives for the plugins that I use frequen ...

I need to retrieve a date value within a function

When using the input type = date, I am trying to retrieve the selected date value in a function. Here is my code: <input style='display:none;' type='date' value='<?php echo date(Y-m-d);?>' name='date' id ...

Tips for Enhancing Window Leveling (Brightness and Contrast)

const pixelArray = [11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11..] if (pixelArray != null) { for (let i = 0; i < pixelArray.length; i++) { let ...

What is the best way to import modules with the "@" symbol in their path when working with Node.js?

Situation In my VueJS project, I have created service modules for use with vue cli. My code makes use of the @ symbol to easily access files within the src folder: /* Inside someService.js */ import API from '@/services/APIService.js' Ch ...

Verify whether the chosen options from a dropdown list coincide with a predetermined value

I am working with a form that generates dropdown lists dynamically. <c:forEach var="companyList" items="${company.detailList}" varStatus="status"> <tr> <td>c:out value="${companyList.employeeName}" /></td> <td> <select ...

AngularJS postback method fails to trigger

Seeking assistance with my angularJS AJAX postback method. Below is the HTML code I have created: <html xmlns="http://www.w3.org/1999/xhtml" ng-app> <head runat="server"> <title></title> <script src="Scripts/angular.js ...

How to format numbers in JavaScript post calculations

Struggling to find a solution to format calculation results with commas for thousand separators (e.g., 10,000). After implementing the .toLocaleString('en-US', {maximumFractionDigits:1}); method to format numbers in the output, I encountered unex ...

Using React hooks to update the state of an array from one component to another

I am currently working on a MERN blog website project and I've encountered an issue while trying to update comments on a post. I'm editing the comment from another component but facing difficulty in updating the state after making changes. Would ...

Navigational paths for the persistent sidebar in Material UI

I am looking to utilize this as a page navigation, but I am struggling with properly linking it to the index. I have attempted separating the items, but that approach did not work as intended. <List> {['Home' , 'Abo ...