在AngularJs中使用控制器的这两种方法有什么区别?
我最近开始使用angularjs,我发现了这两种使用控制器的方法 这个没有给我任何输出在AngularJs中使用控制器的这两种方法有什么区别?,angularjs,Angularjs,我最近开始使用angularjs,我发现了这两种使用控制器的方法 这个没有给我任何输出 <html ng-app=""> <body> <div> Name : <input type="text" ng-model="name"> {{name}} </div><br> <div ng-controller="SimpleController"> <ul>
<html ng-app="">
<body>
<div>
Name : <input type="text" ng-model="name">
{{name}}
</div><br>
<div ng-controller="SimpleController">
<ul>
<li ng-repeat="cust in customers">
{{cust.city}}
</li>
</ul>
</div><br>
<script src = "D://angularJs/angular.min.js"></script>
<script>
function SimpleController($scope){
$scope.customers = [
{names:'Arjun Narahari',city:'Mumbai'},
{names:'Santosh Rangarajan',city:'Pune'},
];
}
</script>
姓名:
{{name}}
-
{{客户城市}
函数SimpleController($scope){
$scope.customers=[
{姓名:'Arjun Narahari',城市:'Mumbai'},
{姓名:'Santosh Rangarajan',城市:'Pune'},
];
}
这个给了我正确的输出
<html ng-app="myApp">
<body>
<div ng-controller="SimpleController">
<ul>
<li ng-repeat="cust in customers">
{{cust.city}}
</li>
</ul>
</div><br>
<script src = "D://angularJs/angular.min.js"></script>
<script>
angular.module("myApp",[]).controller("SimpleController",function($scope){
$scope.customers = [
{names:'Arjun Narahari',city:'Mumbai'},
{names:'Santosh Rangarajan',city:'Pune'},
];
});
</script>
-
{{客户城市}
angular.module(“myApp”,[]).controller(“SimpleControl”,函数($scope){
$scope.customers=[
{姓名:'Arjun Narahari',城市:'Mumbai'},
{姓名:'Santosh Rangarajan',城市:'Pune'},
];
});
输出
孟买
浦那
有人能解释为什么第一种方法不起作用吗?
谢谢你正如你所看到的,第二个有这个额外的代码
angular.module("myApp",[]).
这实际上建立了一个角度。没有它,
函数simpleController()
就没有任何东西使用它第一个版本,使用全局函数,是Angular在很长一段时间内最基本的示例,但从版本1.3起,它已被禁用
现在定义控制器的唯一方法是如第二个示例所示
基本上,第一种方法只用于基础教程。任何规模的应用程序通常都不想占据全球范围,也不想为了更好的模块化而设计。因此,尽管第二个选项让人觉得你在无缘无故地做更多的事情,但随着应用程序的发展,你会感激它 第一种方式已经过时,不再受支持。好的,先生,谢谢您的回复