admin管理员组

文章数量:1322867

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 badges
Add a ment  | 

5 Answers 5

Reset to default 2

The "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 stores. 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