1

I'm trying to use this AngularJS material chip. (CONTACT CHIP - With auto-complete) https://material.angularjs.org/latest/demo/chips

And it has a different structure of what I'm used to. I want to adapt to get the contacts from my mongodb with $http, like:

$http.get("/contacts").success(function(response) {
          contacts = response;
});

But in their Angular Material code example is like this:

angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
      .controller('ContactChipDemoCtrl', DemoCtrl);
  function DemoCtrl ($q, $timeout) { 
...
 function loadContacts() {
  var contacts = [
    'Marina Augustine',
    'Oddr Sarno',
    'Nick Giannopoulos',
    'Narayana Garner',
    'Anita Gros',
    'Megan Smith',
    'Tsvetko Metzger',
    'Hector Simek',
    'Some-guy withalongalastaname'
  ];
}
...

How can I use $http as a parameter for the DemoCtrl function? To get the contacts from db

3
  • where is the codpen link? Commented Jul 19, 2016 at 17:53
  • 2
    Are you just asking how Dependency Injection works with Angular ? I suggest you take a look at the docs docs.angularjs.org/guide/di Commented Jul 19, 2016 at 17:55
  • @Sajeetharan click in the link above and go to CONTACT CHIP, there is a codepen icon Commented Jul 19, 2016 at 17:58

2 Answers 2

2
MyModule.controller("DemoController", DemoCtrl);

DemoCtrl.$inject = ['$http'];

function DemoCtrl ($http) {
     $http.get("/contacts").success(function(response) {
      contacts = response;
});

Above is the preferred way to setup a controller because it doesn't use an anonymous function rather than:

MyModule.controller("DemoController", ['$http', function($http) {
     $http.get("/contacts").success(function(response) {
      contacts = response;
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

i'm gonna try it here
Also - @charlietfl is right - you should also inject $scope and set $scope.contacts = response.data;
Onde question @Booza in my function DemoCtrl there is some parameters DemoCtrl($q,$timeout). When I inject $http this should now be the first parameter, right !?
Whatever the order of the strings are, that should be the order of the parameters. It doesn't matter as long as they match up.
0

You are assigning response data to variable contacts which has no angular scope context

If you are using $scope as your data model for view it would be

$http.get("/contacts").then(function(response) {
          $scope.contacts = response.data;
});

Or if using controllerAs alias in view:

var vm = this
$http.get("/contacts").then(function(response) {
          vm.contacts = response.data;
});

Also need to inject $http in controller

2 Comments

This I know. The question is, how can I use the $http dependency inside the DemoCtrl function, or as a parameter...
You simply inject it. I guess I don't understand problem

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.