0

Im now to AngularJs and i've created a test app. I'm using version 1.3.x and i have faced this problem. Here is the code for HtML and JS

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

function SimpleController($scope) {
    $scope.customers = [
        { name: 'Customer 1', city: 'Gampaha'},
        { name: 'Customer 2', city: 'Negombo'},
        { name: 'Customer 3', city: 'Gampaha'},
        { name: 'Customer 4', city: 'Gampaha'},
        { name: 'Customer 5', city: 'Kadawatha'}
    ];
}
<!DOCTYPE html>
<html lang="en" ng-app="ControllerExample">

<head>
  <meta charset="utf-8">
  <title>AngularJS App</title>
  <link rel="stylesheet" href="css/style.css" />

</head>

<body>
  <div ng-controller="SimpleController">
    Customer Name:
    <input type="text" ng-model="name" />

    <h3>Adding a Simple Controller</h3>
    <ul>
      <li ng-repeat="cust in customers">{{ cust.name | uppercase }} - {{ cust.city }}</li>
    </ul>
  </div>

  <script type="text/javascript" src="js/script.js"></script>
  <script type="text/javascript" src="js/angular.min.js"></script>
</body>

</html>

Expected behavior is that when i run the app it should display the list of customers and when i filter it by entering a name in the textbox it should filter. But it only shows the binding statements. What am i doing wrong here. I've gone through the documentation and used it in my code. Still doesn't work. Thanks in advance.

0

5 Answers 5

2

Syntax error. Just remove the var:

angular.module('ControllerExample', []).controller('SimpleController', SimpleController);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this out:- http://jsfiddle.net/adiioo7/jLLyzm7p/

var myAppModule= angular.module('ControllerExample', []).controller('SimpleController', SimpleController);

function SimpleController($scope) {
    $scope.customers = [{
        name: 'Customer 1',
        city: 'Gampaha'
    }, {
        name: 'Customer 2',
        city: 'Negombo'
    }, {
        name: 'Customer 3',
        city: 'Gampaha'
    }, {
        name: 'Customer 4',
        city: 'Gampaha'
    }, {
        name: 'Customer 5',
        city: 'Kadawatha'
    }];
}

Comments

1

If I well understand you want to filter your customer in function of their names.

You're just missing to use your name with a filter on the ng-repeat.

Ex: Add in your controller

 $scope.nameFilter = function(customer) {
     if (!$scope.name) { // in case of name === null
        return true;
    }

    var searchVal = $scope.name,
        customerName = customer.name;

    var regex = new RegExp('' + searchVal, 'i');

    return regex.test(customerName);
}

Now in your template use

<li ng-repeat="cust in customers | filter:nameFilter"> <!-- List customers that will match with your filter -->
     ....
</li>

Comments

1

Running verison.. You dont need var http://jsfiddle.net/adiioo7/jLLyzm7p/

<body ng-app="ControllerExample">
  <div ng-controller="SimpleController">
    Customer Name:
    <input type="text" ng-model="name" />

    <h3>Adding a Simple Controller</h3>
    <ul>
      <li ng-repeat="cust in customers">{{ cust.name | uppercase }} - {{ cust.city }}</li>
    </ul>
  </div>

  <script type="text/javascript" src="js/script.js"></script>
  <script type="text/javascript" src="js/angular.min.js"></script>
</body>







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

function SimpleController($scope) {
    $scope.customers = [
        { name: 'Customer 1', city: 'Gampaha'},
        { name: 'Customer 2', city: 'Negombo'},
        { name: 'Customer 3', city: 'Gampaha'},
        { name: 'Customer 4', city: 'Gampaha'},
        { name: 'Customer 5', city: 'Kadawatha'}
    ];
}

Comments

0

Try

cust["name"]  cust["city"]

instead of

cust.name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.