简体   繁体   中英

Use Angular with HTML5

I'm taking tutorial in CodeSchool about angular and I try to create my own experiment in html5 and angular new version in vs2012 .

I try to test angular repeat but the problem is HTML5 can't render my angular.

When I try to run my page, the repeater only show the angular statement {{item.name}}:{{item.quantity}} not the result. Why? -"show code below"

Angular script :

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>

application.js :

<script type="text/javascript" src="Scripts/application.js"></script>

 $(function () { var app = angular.module('zaskarCorp', []); app.controller('kirito', function ($scope) { $scope.items = [{ "name": "dual sword", "quantity": 2 }, { "name": "gun", "quantity": 1 }, { "name": "laser sword", "quantity": 1 }]; }); }); 
 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="zaskarCorp"> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script type="text/javascript" src="Scripts/application.js"></script> </head> <body data-ng-controller="kirito"> <ul> <li data-ng-repeat="item in items"> <span>{{item.name}}:{{item.quantity}}</span> </li> </ul> </body> </html> 

As you can see I add data- in my angular because I use html5 . -"I hate warnings"

Your code has a number of typos and errors:

  • funtion instead of function
  • agular instead of angular
  • no jQuery included, but you call $(function(){...})

Here's a plunker with your code working.

Also, for future reference, if you remove .min from the angular source, it makes it easier to debug.

Please add this directive to your body tag: data-ng-app="zaskarCorp"

Also, you have some misspellings in your javascript code.

Without using jQLite your code will look like this:

var app = angular.module('zaskarCorp', []);

app.controller('kirito', function ($scope) {

$scope.items = [
    {
        "name": "dual sword",
        "quantity": 2
    }, 
    {
        "name": "gun",
        "quantity": 1
    }, 
    {
        "name": "laser sword",
        "quantity": 1
    }];
});

Full code:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="zaskarCorp"> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script> var app = angular.module('zaskarCorp', []); app.controller('kirito', function($scope) { $scope.items = [{ "name": "dual sword", "quantity": 2 }, { "name": "gun", "quantity": 1 }, { "name": "laser sword", "quantity": 1 }]; }); </script> </head> <body data-ng-controller="kirito"> <ul> <li data-ng-repeat="item in items"> <span>{{item.name}}:{{item.quantity}}</span> </li> </ul> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM