Tout d'abord, j'espère vraiment pour la compréhension et la tolérance de chacun. Cette question est assez spécifique, alors s'il vous plaît, ne radicalisez plus / vote négatif / mise en attente. Nous sommes tous ici pour apprendre quelque chose de nouveau et partager des connaissances, pas de l'agression. Merci beaucoup.

La question: j'essaye de suivre cet exemple mais cela ne semble rien montrer dans Chrome. Le lien indique ce qui est attendu. Ce que j'obtiens est juste une page vierge dans le navigateur. Quelqu'un peut-il dire exactement pourquoi?

    <!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>
0
Ivegotaquestion 8 mars 2016 à 18:45

3 réponses

Meilleure réponse

Vous utilisez un matériau angulaire, mais vous ne l'importez pas. J'ai mis à jour vos dépendances en fonction de la page de démarrage du matériau angulaire, j'ai créé une < un href = "https://plnkr.co/edit/P3qvamZmynuQeQgy5Asm?p=preview" rel = "nofollow"> plunker pour vérifier cela.

<!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>
1
Sam Bauwens 8 mars 2016 à 16:06

Change ça

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

Pour ça

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

Le nom que vous spécifiez doit correspondre au nom que vous avez mis dans ng-app (myApp dans votre cas)

2
Noshii 8 mars 2016 à 15:52

Il semble que vous n'ayez pas ajouté la référence de ngMaterial.js, vous devez l'ajouter juste après la référence de votre fichier angular.min.js. Prenez-le à partir de ce cdn , ajoutez également CSS avec cela pour obtenir un style au travail.

1
Pankaj Parkar 8 mars 2016 à 16:05