1

I am having a problem loading a set of JSON data with NG CLICK. Without the clickButton function the data loads fine. Is my JSON data structured properly? Here is my fiddle. Click [here] (http://jsfiddle.net/al321/08rjqv4k/3/ "mouse over")

    $scope.items = [
    {
    "Name":"Sam",

    "Address":"Street",

    "Phone":"111",

    "status":"Available"

    }, 

    {
    "Name":"Tom",

    "Address":"Road",

    "Phone":"222",

    "status":"Busy"

    },]

   --js
   var app = angular.module('myApp', []);

    app.controller('myController', 
   ['$scope', '$http', function($scope, $http) {

   $scope.clickButton = function() {
   $http.get('data.json').success(function(data) {
    $scope.items = data.items;
  });
  }

   }]);

2 Answers 2

1

Plunker

JS

var app = angular.module('myApp', []);
app.controller('myController', ['$scope', '$http', function($scope, $http) {
  $scope.clickButton = function() {
    $http.get('data.json').success(function(data) {
        $scope.items = data;
      });
  }

}]);

Markup

  <body ng-controller="myController">
     <div ng-app="myApp">
        <div ng-controller="myController">
          <button ng-click='clickButton()'>Load Data</button>
          <ul ng-repeat="item in items">
            <li ng-repeat="(key, val) in item">{{key}}: {{val}}
            </li>
        </ul>
        </div>
    </div>
  </body>

JSON

[
  {
        "Name":"Sam",

        "Address":"Street",

        "Phone":"111",

        "status":"Available"

    }, 

    {
        "Name":"Tom",

        "Address":"Road",

        "Phone":"222",

        "status":"Busy"

    }
]
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you are trying to accomplish?

Just replace the contents of the clickButton() function with your ajax calls.

You forgot to put the () on ng-click="clickButton(). You need to actually call the function.

Plunker

To add the search functionality you mentioned in the comment, just add a filter to the first ng-repeat:

ng-repeat="item in items| filter: {Name: search}"

Plunker with search

1 Comment

Yes it is. Thanks. I will now attempt to use ng model(search) and tie that to the data. For example: Type Sam in a form, click search and it finds all Sam Json data.

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.