0

I learning AngularJS, and I have some misunderstanding on this example:

(function () {
"use strict";
angular.module("productManagement").controller("ProductListCtrl", ProductListCtrl);

function ProductListCtrl() {
    var vm = this;

    vm.products = [
    {
        "productId": 1,
        "productName": "Leaf Rake",
        "productCode": "GDN-0011",
        "releaseDate": "March 19, 2009",
        "description": "Leaf rake with 48-inch wooden handle.",
        "price": 19.95
    },
    {
        "productId": 2,
        "productName": "Garden Cart",
        "productCode": "GDN-0023",
        "releaseDate": "March 18, 2010",
        "description": "15 gallon capacity rolling garden cart",
        "price": 32.99
    },
     {
         "productId": 5,
         "productName": "Hammer",
         "productCode": "TBX-0048",
         "releaseDate": "May 21, 2013",
         "description": "Curved claw steel hammer",
         "price": 8.99
     },
     {
         "productId": 8,
         "productName": "Saw",
         "productCode": "TBX-0022",
         "releaseDate": "May 15, 2009",
         "description": "15-inch steel blade hand saw",
         "price": 11.55
     },
     {
         "productId": 10,
         "productName": "Video Game Controller",
         "productCode": "GMG-0042",
         "releaseDate": "October 15, 2002",
         "description": "Standard two-button video game controller",
         "price": 35.95
     }
    ];
  }
 }());

What is meaning of this row:

var vm = this;

What is the meaning of this keyword in current context?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael
  • 13,950
  • 57
  • 145
  • 288
  • 1
    In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of. When we define our faithful function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript. An onclick property, though, is owned by the HTML element it belongs to. It's just a saving current context. – Igor Semin Jun 14 '15 at 11:08
  • To add to the previous comment, in the example above, the name of the variable also hints to its use: vm is often used to refer to the view model. Using the variable vm provides more clarity. Anyone looking at the code can now see that when vm is used it refers to the controller's view model context. – jme11 Jun 14 '15 at 11:31
  • possible duplicate of [this vs $scope in AngularJS controllers](http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers) – Pankaj Parkar Jun 14 '15 at 12:11

0 Answers0