gpt4 book ai didi

javascript - Angular : Controller values not getting displayed in html page

转载 作者:行者123 更新时间:2023-11-30 08:40:42 25 4
gpt4 key购买 nike

我刚刚开始学习 angular.js 。我在 app.js 文件中编写了一个基本 Controller 并设置了它的属性。我试图在 html 页面中访问这些数据,但没有成功。 Controller 属性值未在网页中显示。下面是代码:

应用程序.js:

(function(){
var app = angular.module('gemStore',[]);
var gem = {name: 'Diamond', price: 120, description: 'Hard'};
app.controller('StoreController', function() {
this.product = gem;

});



})();

index.html

<!DOCTYPE html>
<html ng-app="gemStore">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" ></script>
<script type="text/javascript" src="js/app.js" ></script>

<p> {{"Hello, Angular!"}}</p>

<div ng-controller="StoreController as store">
<h1>{{store.product.name}}</h1>
<h2>{{store.product.price}}</h2>
<h3>{{store.product.description}}</h3>
</div>
</body>
</html>

请告诉我哪里错了!!

最佳答案

“controller as”语法是在 1.2.0(或附近)添加的,我认为它不是在 1.0.7 中。尝试使用较新版本的 Angular。如果您坚持使用 1.0.7,那么您需要使用 $scope

关于javascript - Angular : Controller values not getting displayed in html page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26734136/

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