javascript - Angular JS - module.controller 参数

标签 javascript angularjs angular-controller angular-module

<分区>

我是 Angular JS 的新手,他们在教程中指出创建 Controller 的方法是:

angular.module('app', [])
  .controller('TodoController', ['$scope', function ($scope) {
    $scope.todos = [
      { title: 'Learn Javascript', completed: true },
      { title: 'Learn Angular.js', completed: false },
      { title: 'Love this tutorial', completed: true },
      { title: 'Learn Javascript design patterns', completed: false },
      { title: 'Build Node.js backend', completed: false },
    ];
  }]);

我想了解每个参数的作用:

  • 'TodoController'
  • 数组
    • '$范围'
    • 功能

我猜第一个是 Controller 的名字,最后一个是TodoController的构造函数。

但是 '$scope' 是什么?在 HTML 上使用的变量名,方法名?

我可以在数组中发送更多参数吗?

我搜索了 Angular 文档,但没有关于方法的文档非常蹩脚。搜索类代码都没有提供更多信息。

最佳答案

在数组中,参数 (String) 是(不包括最后一个,是的,这是构造函数)需要按照数组中的顺序注入(inject)的依赖项,这些将在实现中以相同的顺序传递 Controller (即构造函数)。

$scope 是一个对象,它维护与 View 的绑定(bind),其内容在 View 和 Controller 中都可用。每当您将它注入(inject)新 Controller 并且需要该 Controller 的实例时,它就会由($rootScope 然后是层次结构)的 $new 方法创建被创建。

关于javascript - Angular JS - module.controller 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357441/

相关文章:

AngularJS - 在运行时创建工厂

javascript - Angular JS :controller syntax

angularjs - 从不同的模块访问指令

javascript - 如何在 Angular 中将数据传递到 Controller 并返回到 View ?

javascript - 雅虎财经查询api,想要限制为一个国家的搜索结果(Ionic cordova)

Javascript正则表达式字符串——多行字符串的替换

angularjs - 如何使用 Firebase/AngularFire 从非规范化数据集中加载 (n>2) 级嵌套数据?

javascript - 如何在没有ID或类的情况下访问html元素标签?

javascript - 带有额外参数的触发器和单击事件不起作用

javascript - angularjs 通过 ng-click 传递匿名函数