$ scope未定义

时间:2016-05-01 09:23:16

标签: javascript angularjs

我已经开始学习角度js了。我想通过自定义控制器绑定我的视图。但是在调试时我得到了范围undefined。这是我的代码

<!DOCTYPE html>
<html data-ng-app>
<head>
    <title>Using Angular JS Directives and Data Binding</title>

</head>
    <body data-ng-controller='SimpleController'>
        Name: <br/>
        <input type="text" data-ng-model="name"/>
        <br/>

        <!--<div class="container" data-ng-init="customers=[{name:'Abhishek',city:'Bangalore'},{name:'Sagar',city:'New York'},{name:'Jarvis',city:'Atlanta'}]" >-->
        <div class="container">
            <h3>looping using ng-repeat Directive</h3>
            <ul>
                <li data-ng-repeat="cust in customers | filter:name | orderBy:'city' ">{{ cust.name }} - {{cust.city}}</li>
            </ul>
        </div> 

        <script src="angular.min.js"></script>
        <script>
            function SimpleController($scope){
                $scope.customers = [
                    {name :'Abhishek',city:'Bangalore'},
                    {name:'Sagar',city:'New York'},
                    {name :'Jarvis',city:'Atlanta'}
                ];
            }
        </script> 

    </body>
</html>

获取此错误。 论证&#39; SimpleController&#39;不是一个功能,未定义

1 个答案:

答案 0 :(得分:1)

您尚未在应用中的任何位置声明角度moduleng-app

<强> HTML:

<body ng-app="app"  ng-controller='SimpleController' >

<强> JS:

  var app = angular.module('app', []);
    app.controller('SimpleController',['$scope',function($scope)
    {
       $scope.customers = [
                        {name :'Abhishek',city:'Bangalore'},
                        {name:'Sagar',city:'New York'},
                        {name :'Jarvis',city:'Atlanta'}
                    ];

    }]);

以下是工作 app