AngularJS应用程序主要依靠控制器来控制应用程序中的数据流.使用 ng-controller 指令定义控制器.控制器是一个包含属性/属性和函数的JavaScript对象.每个控制器都接受$ scope作为参数,它指的是控制器需要处理的应用程序/模块.
...
在这里,我们使用ng-controller指令声明一个名为 studentController 的控制器.我们将其定义为以下 :
studentController被定义为一个JavaScript对象,其中$ scope作为参数.
$ scope指的是使用studentController对象的应用程序.
$ scope. student是studentController对象的属性.
firstName和lastName是$ scope.student对象的两个属性.我们将默认值传递给它们.
属性fullName是$ scope.student对象的函数,它返回组合名称.
在fullName函数中,我们获取student对象,然后返回组合名称.
请注意,我们还可以在单独的JS文件中定义控制器对象,并在HTML页面中引用该文件.
现在我们可以使用studentController的学生财产使用ng-model或使用表达式如下<
Enter first name:
Enter last name:
You are entering: {{student.fullName()}}
我们将student.firstName和student.lastname绑定到两个输入框.
我们将student.fullName()绑定到HTML.
现在,无论何时在名字和姓氏输入框中输入任何内容,您都可以看到全名自动更新.
示例
以下示例显示了控制器的使用 :
testAngularJS.htm
Angular JS Controller AngularJS Sample Application
Enter first name:
Enter last name:
You are entering: {{student.fullName()}}
输出
在网络浏览器中打开文件 testAngularJS.htm 并查看结果.