Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

First of all, I really hope for everyone's understanding and tolerance. This question is quite specific, so please no more radicalism / down-voting / putting on hold. We are all here to learn something new and share knowledge, not aggression. Many thanks.

The question: I am trying to follow this example but this does not seem to show anything in Chrome. The link shows what is expected. What I get is just blank page in browser. Can anyone tell exactly why?

    <!DOCTYPE HTML>
<html ng-app="autocompleteDemo">
    <head>
        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
          <![endif]-->
        <title>Shanid KV | AngularJS Dynamic Form Fields</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
        <script type="text/javascript">

              angular
                  .module('autocompleteDemo', ['ngMaterial'])
                  .controller('DemoCtrl', DemoCtrl);

              function DemoCtrl ($timeout, $q, $log) {
                var self = this;
                self.simulateQuery = false;
                self.isDisabled    = false;
                // list of `state` value/display objects
                self.states        = loadAll();
                self.querySearch   = querySearch;
                self.selectedItemChange = selectedItemChange;
                self.searchTextChange   = searchTextChange;
                self.newState = newState;
                function newState(state) {
                  alert("Sorry! You'll need to create a Constituion for " + state + " first!");
                }
                // ******************************
                // Internal methods
                // ******************************
                /**
                 * Search for states... use $timeout to simulate
                 * remote dataservice call.
                 */
                function querySearch (query) {
                  var results = query ? self.states.filter( createFilterFor(query) ) : self.states,
                      deferred;
                  if (self.simulateQuery) {
                    deferred = $q.defer();
                    $timeout(function () { deferred.resolve( results ); }, Math.random() * 1000, false);
                    return deferred.promise;
                  } else {
                    return results;
                  }
                }
                function searchTextChange(text) {
                  $log.info('Text changed to ' + text);
                }
                function selectedItemChange(item) {
                  $log.info('Item changed to ' + JSON.stringify(item));
                }
                /**
                 * Build `states` list of key/value pairs
                 */
                function loadAll() {
                  var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
                          Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
                          Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
                          Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
                          North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
                          South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
                          Wisconsin, Wyoming';
                  return allStates.split(/, +/g).map( function (state) {
                    return {
                      value: state.toLowerCase(),
                      display: state
                    };
                  });
                }
                /**
                 * Create filter function for a query string
                 */
                function createFilterFor(query) {
                  var lowercaseQuery = angular.lowercase(query);
                  return function filterFn(state) {
                    return (state.value.indexOf(lowercaseQuery) === 0);
                  };
                }
              }

        </script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
        <div ng-controller="DemoCtrl as ctrl" layout="column" ng-cloak>
          <md-content class="md-padding">
            <form ng-submit="$event.preventDefault()">
              <p>Use <code>md-autocomplete</code> to search for matches from local or remote data sources.</p>
              <md-autocomplete
                  ng-disabled="ctrl.isDisabled"
                  md-no-cache="ctrl.noCache"
                  md-selected-item="ctrl.selectedItem"
                  md-search-text-change="ctrl.searchTextChange(ctrl.searchText)"
                  md-search-text="ctrl.searchText"
                  md-selected-item-change="ctrl.selectedItemChange(item)"
                  md-items="item in ctrl.querySearch(ctrl.searchText)"
                  md-item-text="item.display"
                  md-min-length="0"
                  placeholder="What is your favorite US state?">
                <md-item-template>
                  <span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
                </md-item-template>
                <md-not-found>
                  No states matching "{{ctrl.searchText}}" were found.
                  <a ng-click="ctrl.newState(ctrl.searchText)">Create a new one!</a>
                </md-not-found>
              </md-autocomplete>
              <br/>
              <md-checkbox ng-model="ctrl.simulateQuery">Simulate query for results?</md-checkbox>
              <md-checkbox ng-model="ctrl.noCache">Disable caching of queries?</md-checkbox>
              <md-checkbox ng-model="ctrl.isDisabled">Disable the input?</md-checkbox>
              <p>By default, <code>md-autocomplete</code> will cache results when performing a query.  After the initial call is performed, it will use the cached results to eliminate unnecessary server requests or lookup logic. This can be disabled above.</p>
            </form>
          </md-content>
        </div>

    </body>
</html>
share|improve this question
up vote 1 down vote accepted

You use angular material, but you don't import it. I updated your dependencies according to the angular material getting started page, I created a plunker to verify this.

<!DOCTYPE html>
<html ng-app="myapp">
    <head>
        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
          <![endif]-->
        <title>Shanid KV | AngularJS Dynamic Form Fields</title>
        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>

  <!-- Angular Material Library -->
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>

        <script type="text/javascript">

              angular
                  .module('myapp', ['ngMaterial'])
                  .controller('DemoCtrl', DemoCtrl);

              function DemoCtrl ($timeout, $q, $log) {
                var self = this;
                self.simulateQuery = false;
                self.isDisabled    = false;
                // list of `state` value/display objects
                self.states        = loadAll();
                self.querySearch   = querySearch;
                self.selectedItemChange = selectedItemChange;
                self.searchTextChange   = searchTextChange;
                self.newState = newState;
                function newState(state) {
                  alert("Sorry! You'll need to create a Constituion for " + state + " first!");
                }
                // ******************************
                // Internal methods
                // ******************************
                /**
                 * Search for states... use $timeout to simulate
                 * remote dataservice call.
                 */
                function querySearch (query) {
                  var results = query ? self.states.filter( createFilterFor(query) ) : self.states,
                      deferred;
                  if (self.simulateQuery) {
                    deferred = $q.defer();
                    $timeout(function () { deferred.resolve( results ); }, Math.random() * 1000, false);
                    return deferred.promise;
                  } else {
                    return results;
                  }
                }
                function searchTextChange(text) {
                  $log.info('Text changed to ' + text);
                }
                function selectedItemChange(item) {
                  $log.info('Item changed to ' + JSON.stringify(item));
                }
                /**
                 * Build `states` list of key/value pairs
                 */
                function loadAll() {
                  var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
                          Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
                          Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
                          Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
                          North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
                          South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
                          Wisconsin, Wyoming';
                  return allStates.split(/, +/g).map( function (state) {
                    return {
                      value: state.toLowerCase(),
                      display: state
                    };
                  });
                }
                /**
                 * Create filter function for a query string
                 */
                function createFilterFor(query) {
                  var lowercaseQuery = angular.lowercase(query);
                  return function filterFn(state) {
                    return (state.value.indexOf(lowercaseQuery) === 0);
                  };
                }
              }

        </script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
        <div ng-controller="DemoCtrl as ctrl" layout="column" ng-cloak>
          <md-content class="md-padding">
            <form ng-submit="$event.preventDefault()">
              <p>Use <code>md-autocomplete</code> to search for matches from local or remote data sources.</p>
              <md-autocomplete
                  ng-disabled="ctrl.isDisabled"
                  md-no-cache="ctrl.noCache"
                  md-selected-item="ctrl.selectedItem"
                  md-search-text-change="ctrl.searchTextChange(ctrl.searchText)"
                  md-search-text="ctrl.searchText"
                  md-selected-item-change="ctrl.selectedItemChange(item)"
                  md-items="item in ctrl.querySearch(ctrl.searchText)"
                  md-item-text="item.display"
                  md-min-length="0"
                  placeholder="What is your favorite US state?">
                <md-item-template>
                  <span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
                </md-item-template>
                <md-not-found>
                  No states matching "{{ctrl.searchText}}" were found.
                  <a ng-click="ctrl.newState(ctrl.searchText)">Create a new one!</a>
                </md-not-found>
              </md-autocomplete>
              <br/>
              <md-checkbox ng-model="ctrl.simulateQuery">Simulate query for results?</md-checkbox>
              <md-checkbox ng-model="ctrl.noCache">Disable caching of queries?</md-checkbox>
              <md-checkbox ng-model="ctrl.isDisabled">Disable the input?</md-checkbox>
              <p>By default, <code>md-autocomplete</code> will cache results when performing a query.  After the initial call is performed, it will use the cached results to eliminate unnecessary server requests or lookup logic. This can be disabled above.</p>
            </form>
          </md-content>
        </div>

    </body>
</html>
share|improve this answer
    
That works, many thanks. – Ivegotaquestion 2 days ago
    
The only thing which does not seem to work is function newState, I never get alert() when I click the link. Can you spot why? – Ivegotaquestion yesterday
    
I think it comes from the fact that the md-not-found item gets the click and it doesn't go through to the link. – Sam Bauwens yesterday
    
Do you know how to fix that? – Ivegotaquestion yesterday

Change this

.module('autocompleteDemo', ['ngMaterial'])

to this

.module('myapp', ['ngMaterial'])

The name you specify has to match the name you put in ng-app (myApp in your case)

share|improve this answer
    
Well spotted, done but result is still the same I am afraid. Let me update the post. – Ivegotaquestion 2 days ago
    
Do you got any errors when you open the console? Update your answer if this is the case – Noshii 2 days ago
    
I got angular.js:36 Uncaught Error: [$injector:modulerr] errors.angularjs.org/1.2.25/$injector/……glea‌​pis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.25%2Fangular.min.js%3A18%3A170) – Ivegotaquestion 2 days ago

Seems that you missed to add the reference of ngMaterial.js, you need to add that right after your angular.min.js file file reference. Take it from this cdn, also add CSS with that to get styling in work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.