AngularJS简易应用网页停止渲染问题求助
Fixing AngularJS Blank Page Issue in Your Code
Hey there, let's figure out why you're getting those blank pages! I’ve spotted a few key issues in your code that are causing AngularJS to fail rendering your content:
HTML Structure & Attribute Errors
- Incorrect
<html>tag placement: You added a second<html lang="en-US">tag inside your<head>section, which breaks proper HTML document structure. Move thelangattribute to your root<html>tag instead, and remove the extra<html>tag from the head. - Invalid stylesheet
relattribute: Your link tag usesrel="stylesheet/css"— this is wrong. The correct value for stylesheet links isrel="stylesheet".
Data Binding Mismatch (Core Issue)
This is the main reason your page is blank:
- In your controller, you assigned the
gemsarray tothis.product(singular), but your HTML usesng-repeat="product in store.products"(plural) to loop through items. Angular can’t find theproductsproperty on your controller, so it has nothing to render. - Additionally, inside the
ng-repeatblock, you’re trying to accessstore.product.name, but once you’re in the repeat loop, you should use the localproductvariable (fromproduct in store.products) to reference each item’s properties.
Fixed HTML Code
<!DOCTYPE html> <html ng-app='store' lang="en-US"> <head> <link rel="stylesheet" type="text/css" href="bootstrap.min.css" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> </head> <body ng-controller="StoreController as store"> <div ng-repeat="product in store.products"> <h1> {{product.name}} </h1> <h2> {{product.price}} </h2> <p> {{product.description}} </p> <button>Add to Cart</button> </div> <script type="text/javascript" src="app.js"></script> </body> </html>
Fixed JavaScript Code
(function (){ const app = angular.module('store', []); app.controller("StoreController", function(){ this.products = gems; // Updated to plural to match HTML binding }); const gems = [ { name: "Dodecahedron", price: 2.95, description: 'Many sided gem. Would look great in a ring.', }, { name: "Pentagonal Gem", price: 5.95, description: 'A gem shaped like a pentagon. More expensive and more shiny', } ]; })();
Pro Tip for Future Debugging
Whenever you hit a blank page with AngularJS, open your browser’s developer tools (press F12) and check the Console tab. Angular will almost always log an error message that points directly to the problem — this will save you tons of time troubleshooting!
内容的提问来源于stack exchange,提问作者breakthatbass
相关产品推荐
相关产品推荐

