$ scope未在AngularJS练习中定义

时间:2015-04-17 22:38:02

标签: angularjs

我跟随Dan Wahlin" AngularJS Fundamentals in 60-ish Minutes"在YouTube上。根据AngularJS 1.2及之后的更改,我添加了对" angular-route.min.js"的调用。 (并且我知道我需要在下面使用最小安全语法),包括' ngRoute'作为模块依赖项,并将$ scope添加为控制器依赖项。代码运行时没有错误,直到它到达底部的$ scope.addCustomer()函数,此时它会抛出" $ scope未定义"错误。我看过类似的帖子,但没有看到同样的问题。我如何使用addCustomer()来使它包含在$ scope对象中,min-safe,并且不会抛出错误?自定义函数是否也需要依赖注入(如果甚至可以这样做)?

<!doctype HTML>
<html lang="en" ng-app="demoApp">
    <!--- This is how you integrate with partials (views)... --->
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.min.js"></script>
    </head>
    <body>
        <div>
            <!-- placeholder for the views -->
            <div ng-view></div>

            <!-- normally would put this in it's own js file and call with script src="" -->
            <script>
                var demoApp = angular.module('demoApp', ['ngRoute']);

                demoApp.config(function ($routeProvider) {
                    $routeProvider
                    .when("/",
                            {
                                controller:"simpleController",
                                templateURL:"partials/view1.htm"
                            })
                    .when("/partial2",
                            {
                                controller:"simpleController",
                                templateURL:"partials/view2.htm"
                            })
                    .otherwise({ redirectTo: "/"});
                });


                demoApp.controller('simpleController', ['$scope', function(sc) {
                    sc.customers = [
                            {name:"David Jones", city:"Phoenix"},
                            {name:"James Riley", city:"Atlanta"},
                            {name:"Heedy Wahlin", city:"Chandler"},
                            {name:"Thomas Winter", city:"Seattle"}
                        ];
                }]);

                $scope.addCustomer = function(){
                    $scope.customers.push(
                        {name: $scope.newCustomer.name, city: $scope.newCustomer.city}
                    );
                }
            </script>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

$scope函数在控制器内部定义(范围存在)。

您的addCustomer函数应该进入注入范围的控制器内。

以下是它的外观:

demoApp.controller('simpleController', ['$scope', function($scope) {
     $scope.customers = [
             {name:"David Jones", city:"Phoenix"},
             {name:"James Riley", city:"Atlanta"},
             {name:"Heedy Wahlin", city:"Chandler"},
             {name:"Thomas Winter", city:"Seattle"}
         ];

     $scope.addCustomer = function() {
         $scope.customers.push(
             {name: $scope.newCustomer.name, city: $scope.newCustomer.city}
         );
     };
 }]);