javascript - Angular JS function not defined error -
i learning angularjs angularjs.org video tutorials when run code says
error: [ng:areq] argument 'reviewcontroller' not function
i got undefined in console code looks same in video here html
<!doctype html> <html ng-app="store"> <title>testing angularjs</title> <head> <link rel="stylesheet" type="text/css" href="bootstrap.css"/> <script type="text/javascript" src="js/angular.js"></script> <script type="text/javascript" src="app.js"></script> </head> <body ng-controller="storecontroller store"> <ul class="list-group"> <li class="list-group-item" ng-repeat="product in store.products"> <h3>{{product.name}} <em class="pull-right">{{product.price | currency}}</em> <img class="gallery" ng-src="{{product.images[0].full}}"/> </h3> <section ng-controller="panelcontroller panel"> <ul class="nav nav-pills"> <li ng-class="{active: panel.isselected(1)}"><a href ng-click="panel.selecttab(1)">description</a></li> <li ng-class="{active: panel.isselected(2)}"><a href ng-click="panel.selecttab(2)">specifications</a> </li> <li ng-class="{active: panel.isselected(3)}"><a href ng-click="panel.selecttab(3)">reviews</a></li> </ul> <div class="panel" ng-show="panel.isselected(1)"> <h3> description</h3> <h4>{{product.description}}</h4> </div> <div class="panel" ng-show="panel.isselected(2)"> <h3> specifications</h3> <blockquote>none yet</blockquote> </div> <div class="panel" ng-show="panel.isselected(3)"> <h3> reviews</h3> <blockquote ng-repeat="review in product.reviews"> <b>stars: {{review.stars}}</b> <p>{{review.body}}</p> <cite>_{{review.author}}</cite> </blockquote> <form name="reviewform" ng-controller="reviewcontroller reviewctrl"> <blockquote> <b>stars: {{reviewctrl.review.stars}}</b> <p>{{reviewctrl.review.body}}</p> <cite>_{{reviewctrl.review.author}}</cite> </blockquote> <label for="select">select</label> <select id="select" ng-model="reviewctrl.review.stars"> <option value="1">1 star</option> <option value="2">2 star</option> <option value="3">3 star</option> </select> <label for="comments"></label> <textarea id="comments" ng-model="reviewctrl.review.body"></textarea> <label for="email">email</label> <input ng-model="reviewctrl.review.author" type="email" id="email"/> <input type="submit" value="submit"/> </form> </div> </section> </li> </ul> </body> </html>
here app.js file
(function () { var app = angular.module('store', []); app.controller('storecontroller', function () { this.products = [ { name: 'cup', price: 2.95, description: 'it best buy fast', images: [ { full: 'gem.jpeg' } ], reviews: [ { stars: 5, body: 'this best gem', author: 'dadsa@dasasd.com' }, { stars: 1, body: 'easily broken', author: 'zzzfasdaadsa@dasasd.com' } ] }, { name: 'bottle', price: 1.12, description: 'it\'s best bottle', images: [ { full: 'gem.jpeg' } ], reviews: [ { stars: 2, body: 'latest gem', author: 'aasdsa@dasasd.com' } ] } ]; // product property of controller }); app.controller("reviewcontroller", function () { this.review = {}; }); app.controller("panelcontroller", function () { this.tab = 1; this.selecttab = function (settab) { this.tab = settab; }; this.isselected = function (checktab) { return this.tab === checktab; }; }); })();
the problem here:
<script type="text/javascript" src="js/angular.js"></script> <script type="text/javascript" src="app.js"></script>
scripts executed in order encountered on page. angular.js
runs before app.js
. parse document , try resolve ng-app
(and thereby ng-controller
) before app.js
has chance execute.
there 2 easy ways solve this:
put app.js
inline @ end of document
although did scripts executed in order encountered, angularjs puts initialization code behind document.isready
. it's loaded, have access angular.module
, won't execute bootstrap logic until after own code has finished loading. solution fine tiny projects, isn't great when code grows large.
bootstrap application manually
remove ng-app="store"
html code, , @ end of app.js
, add following code
angular.element(document).ready(function() { angular.bootstrap(document, ['store']); });`
if work angular long enough, used second solution, helps play nicely dynamic loaders such $script, requirejs, et al -- not mention example you've posted!
Comments
Post a Comment