Why is the variable name “$scope” necessary ?

后端 未结 3 1278
遇见更好的自我
遇见更好的自我 2021-01-04 18:07

I\'m fairly new to Javascript ( just finished the book Eloquent Javascript ), and am currently reading AngularJS from O\'Reilly. And getting this small snippet of code to w

3条回答
  •  没有蜡笔的小新
    2021-01-04 18:22

    Both previous answers are correct , just you should know that you can "override" the default behavior if you're declaring a controller this way:

    module.controller("ControllerName",["$scope",function( custom_name ){ ... }]);
    

    Example:

    var app = angular.module("myApp",[]);
    
    app.controller("TextController",["$scope",function(glue){
        glue.name1 = "John";
        glue.name2 = "Paul";
        glue.name3 = "George";
        glue.name4 = "Ringo";
    }]);
    

    and then:

    Hello {{ name1 }}, {{ name2 }}, {{ name3 }}, {{ name4 }}!

    Working here: http://jsfiddle.net/d4M2P/

提交回复
热议问题