5

我是角度 js 的新手

控制器在我的代码中无法正常工作

我正在尝试运行以下代码

<!DOCTYPE html>
<html data-ng-app >
<head>
    <title>Using AngularJS Directives and Data binding</title>
</head>
<body>

    name 
    <br />

    <div data-ng-controller="SimpleController">
    <input type="text"  data-ng-model="name"/>{{name}}

    <ul>
    <li data-ng-repeat="cust in customers | filter:name | orderBy:city"> {{cust.name | uppercase}}-{{cust.city | lowercase}} </li>
    </ul>
    </div>

    <script src= "angular/angular.min.js"></script>

    <script>

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

        demoApp.controller('SimpleController', function($scope){
        $scope.customers=[
        {name:'John Smith', city:'kashipur'},
        {name:'John fds' , city:'san francisko'},
        { name:'shubham', city:'giarhineg'},
        {name:'batra', city:'world'}];
        });
    </script>
</body>
</html>
</html>

但它没有给出欲望输出。

如果我做错了什么,请告诉我。

4

2 回答 2

4

您缺少在ng-app指令中添加值来告诉 AngulardemoApp在页面上运行模块。

ng-app="demoApp"
于 2015-06-06T21:14:39.430 回答
1

您应该添加您的应用程序的名称。

ng-app="demoApp"

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

  demoApp.controller('SimpleController', function($scope) {
    $scope.customers = [{
      name: 'John Smith',
      city: 'kashipur'
    }, {
      name: 'John fds',
      city: 'san francisko'
    }, {
      name: 'shubham',
      city: 'giarhineg'
    }, {
      name: 'batra',
      city: 'world'
    }];
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp">
  name
  <br />

  <div data-ng-controller="SimpleController">
    <input type="text" data-ng-model="name" />{{name}}

    <ul>
      <li data-ng-repeat="cust in customers | filter:name | orderBy:city">{{cust.name | uppercase}}-{{cust.city | lowercase}}</li>
    </ul>
  </div>
</div>

于 2015-06-06T21:15:52.300 回答