Angularjs $scope和#x27之间的混淆;控制器中的s及其功能?

Angularjs $scope和#x27之间的混淆;控制器中的s及其功能?,angularjs,Angularjs,我不熟悉UI。我确实对AngularJS中的$scope感到困惑。请参考下面的代码片段 var mainApp = angular.module("mainApp", []); mainApp.controller(['$scope', function($scope) { $scope.name = "John"; }]); 那么,$scope和函数($scope)之间有什么区别呢?我们如何将两者联系起来?是否需要有$scope参数?请举例说明。我真的很感激 谢谢, John1

我不熟悉UI。我确实对AngularJS中的
$scope
感到困惑。请参考下面的代码片段

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

mainApp.controller(['$scope', function($scope) {

    $scope.name = "John";

}]);
那么,
$scope
函数($scope)
之间有什么区别呢?我们如何将两者联系起来?是否需要有
$scope
参数?请举例说明。我真的很感激

谢谢,
John

1.当您应用以下角度JS代码的缩小时:

var mainApp = angular.module("mainApp", []);
mainApp.controller(['$scope','$log', function($scope,$log) {
$scope.name = "John";
$log.log("John");
}]);
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($scope,$log) {
$scope.name = "John";
$log.log("John");
});
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($log,$scope) {
$scope.name = "John";
$log.log("John");
});
缩小版:

var mainApp=angular.module("mainApp",
[]);mainApp.controller(["$scope","$log",function(n,o)
{n.name="John",o.log("John")}]);
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
2.应用以下角度JS代码的缩小时:

var mainApp = angular.module("mainApp", []);
mainApp.controller(['$scope','$log', function($scope,$log) {
$scope.name = "John";
$log.log("John");
}]);
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($scope,$log) {
$scope.name = "John";
$log.log("John");
});
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($log,$scope) {
$scope.name = "John";
$log.log("John");
});
缩小版:

var mainApp=angular.module("mainApp",
[]);mainApp.controller(["$scope","$log",function(n,o)
{n.name="John",o.log("John")}]);
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
3.应用以下角度JS代码的缩小时:

var mainApp = angular.module("mainApp", []);
mainApp.controller(['$scope','$log', function($scope,$log) {
$scope.name = "John";
$log.log("John");
}]);
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($scope,$log) {
$scope.name = "John";
$log.log("John");
});
var mainApp = angular.module("mainApp", []);
mainApp.controller(function($log,$scope) {
$scope.name = "John";
$log.log("John");
});
缩小版:

var mainApp=angular.module("mainApp",
[]);mainApp.controller(["$scope","$log",function(n,o)
{n.name="John",o.log("John")}]);
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
var mainApp=angular.module("mainApp",[]);mainApp.controller(function(n,a)
{n.name="John",a.log("John")});
在Ex-2和Ex-3中,您会注意到您交换了$scope和$log的依赖位置,然后您的缩小版本也是相同的,这将给您带来
依赖注入错误
,因此我们将字符串值放置为字符串值,因为您可以在Ex-1中看到,无法缩小字符串值

每次定义控制器时不需要$scope,但$scope提供了重要的功能,如绑定HTML(视图)和JavaScript(控制器)。。

$scope
函数($scope)
之间有什么区别

当你这样做的时候

  mainApp
  .controller(
    ['$scope', //line 1
      function($scope) //line 2
      {
      }
    ]);
  • 第1行
    中,它指的是,它是指应用程序模型的对象
  • 第2行
    中,它是注入(上面提到的)$scope对象的变量(也可以方便地称为$scope)。此变量可以有任何其他名称,
    $scope
    用于在整个代码中保留提示性引用
例如,如果我将其名称更改为
myFunnyScope
,您的示例也会起作用,如下所示:

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

mainApp.controller(['$scope', function(myFunnyScope) {
    myFunnyScope.name = "John";
}]);
我们如何将两者联系起来

参考我之前发布的代码片段,可以看出$scope对象正在
myFunnyScope
变量中被注入,这意味着您使用
myFunnyScope
就像它是
$scope
本身一样

是否需要有
$scope
参数

只要您需要访问所提到的$scope对象提供的所有好处,当您这样做时,就需要将对象(
[$scope,
)注入持有者(
函数($scope){…
)为了不让AngularJS应用程序崩溃。否则,不需要注入对象,但必须在函数参数中显式调用它
$scope
,以便AngularJS知道它必须在其中注入
$scope
对象。此规则不仅适用于
$scope
,而且适用于所有其他AngularJS相关服务、工厂等,如
$timeout
$window$
$location


您可能想阅读并考虑使用语法来解释原因,如果您不想直接使用<代码> $Simule。

它称为依赖注入。请参阅文档。