gpt4 book ai didi

javascript - 简单的 Angular 应用程序无法正常工作

转载 作者:行者123 更新时间:2023-11-28 11:15:57 25 4
gpt4 key购买 nike

简单的 Angular 应用程序无法工作

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script>
angular.module('myApp',[]).controller('SiteController', function($scope)
{
$scope.publisher ='sitePoint';
$scope.type="Web Development";
$scope.name ="Scope for site controller";
});
angular.module('myApp',[]).controller('BookController',function($scope)
{
$scope.books=['Jump Start HTML5','Jump Start CSS','Jump Start Responsive Web Design'];
$scope.name = "Scope for book controller";
});
</script>
</head>
<body ng-app="myApp" ng-controller="SiteController">
<span>{{publisher}} excels in {{type}} books</span>
<div ng-controller="BookController">
<h3>Some of the popular books from {{publisher}}</h3>
<ul>
<li ng-repeat="book in books">
{{book}}
</li>
</ul>
</div>
</body>
</html>

是因为我声明了两次 angular.module 吗?请查看plunker我错过了什么?

最佳答案

您不需要在此处将数组设置为第二个参数angular.module('myApp',[])。此语法意味着您正在定义一个新模块。

您可以像这样链接 Controller :

        angular.module('myApp',[]).controller('SiteController', function($scope)
{
$scope.publisher ='sitePoint';
$scope.type="Web Development";
$scope.name ="Scope for site controller";
})
.controller('BookController',function($scope)
{
$scope.books=['Jump Start HTML5','Jump Start CSS','Jump Start Responsive Web Design'];
$scope.name = "Scope for book controller";
});

或者,只使用 getter angular.module('myApp'),不带第二个参数

        angular.module('myApp',[]).controller('SiteController', function($scope)
{
$scope.publisher ='sitePoint';
$scope.type="Web Development";
$scope.name ="Scope for site controller";
});
angular.module('myApp').controller('BookController',function($scope)
{
$scope.books=['Jump Start HTML5','Jump Start CSS','Jump Start Responsive Web Design'];
$scope.name = "Scope for book controller";
});

关于javascript - 简单的 Angular 应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30863023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com