admin管理员组文章数量:1322535
I've just started learning angular.js . I wrote a basic controller in app.js file and set its property. I'm trying to access this data inside html page but in vain. Controller property values are not getting shown in webpage. Below is the code :
app.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=".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>
Please tell me where am i going wrong!!
I've just started learning angular.js . I wrote a basic controller in app.js file and set its property. I'm trying to access this data inside html page but in vain. Controller property values are not getting shown in webpage. Below is the code :
app.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./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>
Please tell me where am i going wrong!!
Share Improve this question asked Nov 4, 2014 at 11:34 Manisha Manisha 8156 gold badges15 silver badges32 bronze badges5 Answers
Reset to default 2The "controller as"-syntax was added in 1.2.0 (or thereabouts), I don't think it was in 1.0.7. Try using a newer version of Angular. If you're stuck with 1.0.7 then you need to use $scope
.
Your AngularJS version doesn't supports StoreController as store
syntax. Change it to StoreController
and simply remove the store
s. Also you must inject
the scope
object into the controller and set its properties instead.
Here is the updated and working code.
var app = angular.module('gemStore', []);
var gem = {
name: 'Diamond',
price: 120,
description: 'Hard'
};
app.controller('StoreController', ['$scope',
function($scope) {
$scope.product = gem;
}
]);
<!DOCTYPE html>
<html ng-app="gemStore">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script src="https://ajax.googleapis./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">
<h1>{{product.name}}</h1>
<h2>{{product.price}}</h2>
<h3>{{product.description}}</h3>
</div>
</body>
</html>
The controller as syntax in angular was introduced in version 1.2 and you are using version 1.0.7. If you update the version of angular in your script tags to 1.2 or above it will work fine.
<script type="text/javascript" src="https://ajax.googleapis./ajax/libs/angularjs/1.3.0/angular.min.js" ></script>
You can also see this working fine on the following link: http://jsbin./tadinesime/2/edit
I hope this helps.
Set
ng-app="gemStore"
on the body (or in whatever html tag which contains your angularjs code)
here's the jsfiddle: http://jsfiddle/49yrj986/
this is a course on http://campus.codeschool./courses/shaping-up-with-angular-js/level/1/section/2/our-first-controller if someone else wants to try these tutorials
本文标签: javascriptAngularjsController values not getting displayed in html pageStack Overflow
版权声明:本文标题:javascript - Angularjs : Controller values not getting displayed in html page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109567a2421181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论