AngularJS app无法正常运行

时间:2013-10-25 13:16:01

标签: javascript angularjs

我是AngularJS的新手。我试图从一本书中运行一个简单的例子,但它没有正常工作,我无法弄清楚为什么。

此代码运行良好:

<html ng-app>
<head>
    <script src="angular.js"></script>
    <meta charset="UTF-8">
    <title>Angular </title>
</head>
<body>
    <div ng-controller="HelloController">
        <input ng-model="greeting.text"/>
        <p>{{greeting.text}}, World!</p>
    </div>
    <script src="angular.js"></script>
    <script>
             function HelloController($scope) {
                 $scope.greeting = {text: 'Hello'};
             }
    </script>
</body>
</html>

enter image description here

但这是我遇到问题的代码

<html ng-app='myApp'>
<head>
    <title>Shopping Cart</title>
    <script src="angular.js"></script>
</head>
<body ng-controller='CartController'>
    <h1>Your Order</h1>
    <div ng-repeat="item in items">
        <span>{{item.title}}</span>
        <input ng-model="item.quantity">
        <span>{{item.price| currency}}</span>
        <span>{{item.price * item.quantity| currency}}</span>
        <button ng-click="remove($index)">Remove</button>
    </div>
    <script src="angular.js"></script>
    <script>
                function CartController($scope) {
                    $scope.items = [
                        {title: 'Paint pots', quantity: 8, price: 3.95},
                        {title: 'Polka dots', quantity: 17, price: 12.95},
                        {title: 'Pebbles', quantity: 5, price: 6.95}
                    ];
                    $scope.remove = function(index) {
                        $scope.items.splice(index, 1);
                    };
                }
    </script>
</body>
</html>

期待此输出:

enter image description here

但得到这个输出:

enter image description here

我不明白为什么我没有得到数据输出,为什么它不重复。基本上,为什么示例没有运行。我直接从书中复制并粘贴代码。

6 个答案:

答案 0 :(得分:18)

当你写ng-app='myApp'时,你会说某个名为myApp的模块存在于角度。

只需在控制器定义之前添加此行:

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

您可以看到文档herehere

答案 1 :(得分:6)

您应该定义myApp模块:

<html ng-app='myApp'>
<head>
    <title>Your Shopping Cart</title>
</head>
<body ng-controller='CartController'>
    <h1>Your Order</h1>
    <div ng-repeat='item in items'>
        <span>{{item.title}}</span>
        <input ng-model='item.quantity'>
        <span>{{item.price | currency}}</span>
        <span>{{item.price * item.quantity | currency}}</span>
        <button ng-click="remove($index)">Remove</button>
    </div>
    <script src="lib/angular.js"></script>
    <script>
        var app = angular.module('myApp', []);
        app.controller('CartController', ['$scope', function($scope) {
            $scope.items = [
                {title: 'Paint pots', quantity: 8, price: 3.95},
                {title: 'Polka dots', quantity: 17, price: 12.95},
                {title: 'Pebbles', quantity: 5, price: 6.95}
            ];
            $scope.remove = function(index) {
                $scope.items.splice(index, 1);
            }
        }]);
    </script>
  </body>
</html>

答案 2 :(得分:0)

或者您也可以添加命名空间:

<html lang="en" ng-app="myapp" xmlns:ng="http://angularjs.org">

即使添加模块是最好的方法。

答案 3 :(得分:0)

将app的名称命名为html ng-app =&#34; myapp&#34;在html文件中然后将模块添加/定义到控制器中 var myapp = angular.module(&#39; myapp&#39;,[]);

答案 4 :(得分:0)

即使我遇到这个问题,也可以通过在控制器js之前调用模块创建范围来解决它。还要确保在脚本或js文件中创建模块。

答案 5 :(得分:0)

我修好了。

<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<script src="angular-1.5.3/angular.js"></script>
<script>
var myApp = angular.module('myApp',[])

myApp.controller('CartController', function($scope) {
            $scope.items = [
            {title: 'Paint pots', quantity: 8, price: 3.95},
            {title: 'Polka Dots', quantity: 17, price: 12.95},
            {title: 'Pebbles', quantity: 5, price: 6.95}
        ];

    $scope.remove = function(index){
        $scope.items.splice(index, 1);
    };
});
</script>   
</head>
<body ng-controller="CartController">
<h1>Your order</h1>

<div ng-repeat="item in items">
    <span>{{item.title}}</span>
    <input ng-model="item.quantity" />
    <span>{{item.price | currency}}</span>
    <span>{{item.price * item.quantity | currency}}</span>
    <button ng-click="remove($index)">Remove</button>
</div>
</body>
</html>