简体   繁体   中英

Angular JS: Controller not working. all things work perfect when i try with ng-init

I am new to AngularJS. I cannot get my controller to work. However, ng-init works perfectly. This is the code:

<div data-ng-controller="SimpleController">
  <input type="text" data-ng-model="yourName" placeholder="Enter a name here">
  <h3>
    <ul> 
      <li data-ng-repeat="cust in customers | filter:yourName | orderBy : 'name'"> {{ cust.name | uppercase }} - {{ cust.city | uppercase }} </li>
    </ul>
  </h3>
</div>
<script src="js/angular.min.js"></script>
<script>
  function SimpleController($scope) {
    $scope.customers = [{
      name: 'rutunj',
      city: 'surat'
    }, {
      name: 'aushik',
      city: 'Jugal'
    }, {
      name: 'kushik',
      city: 'Lugal'
    }];
  }
</script>

In script you have done this

function SimpleController($scope) {
  $scope.customers = [{
    name: 'rutunj',
    city: 'surat'
  }, {
    name: 'aushik',
    city: 'Jugal'
  }, {
    name: 'kushik',
    city: 'Lugal'
  }];
}

But you haven't define angular module so change code as below inside script.

angular.module('myApp', []).controller('SimpleController', SimpleController);

function SimpleController($scope)
  // Controller code
}

and add ng-app in div like

<div ng-app="myApp" data-ng-controller="SimpleController">

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