首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角js函数不返回在自动完成时显示的值。

角js函数不返回在自动完成时显示的值。
EN

Stack Overflow用户
提问于 2016-09-25 22:44:18
回答 1查看 71关注 0票数 1

下面是我的角脚本,它完成了一个自动完成:

代码语言:javascript
复制
angular.module( "myApp", ['ngAutocomplete']).controller("myController",function ($scope) {

                        $scope.result1 = '';
                        $scope.options1 = null;
                        $scope.details1 = '';


                        $scope.products = [];
                            $scope.addItem = function () {
                                $scope.products.push($scope.addMe);
                            }


                  });



                    angular.module( "ngAutocomplete", []).directive('ngAutocomplete', function($parse) {

                        return {

                          scope: {
                            details: '=',
                            ngAutocomplete: '=',
                            options: '='
                          },

                          link: function(scope, element, attrs, model) {
                            var opts

                            var initOpts = function() {

                              opts = {}
                              if (scope.options) {
                                if (scope.options.types) {
                                  opts.types = []
                                  opts.types.push(scope.options.types)
                                }
                                if (scope.options.bounds) {
                                  opts.bounds = scope.options.bounds
                                }
                                if (scope.options.country) {
                                  opts.componentRestrictions = {
                                    country: scope.options.country
                                  }
                                }
                              }
                            }
                            initOpts()


                            var newAutocomplete = function() {
                              scope.gPlace = new google.maps.places.Autocomplete(element[0], opts);
                              google.maps.event.addListener(scope.gPlace, 'place_changed', function() {
                                scope.$apply(function() {

                                    scope.details = scope.gPlace.getPlace();

                                  scope.ngAutocomplete = element.val();
                                });
                              })
                            }
                            newAutocomplete()



                          }
                        };
                      });

使用js的HTML标记是:

代码语言:javascript
复制
  <input type="text" class="form-control" id="Autocomplete" placeholder="Enter Airport Code or City Name" ng-autocomplete="result1" details="details1" options="options1" ng-model="addMe">
   <button type="submit" class="btn btn-default" ng-click="addItem()">Add to Itinerary</button>

                <ul>
                    <li ng-repeat="x in products">{{result1}}</li>
                </ul>

这会将新调用的值添加到添加到的每个值中,但替换前面的值。

意思:

发生了什么:

首先,我添加了:旧金山->列表:

  • 旧金山

第二,我加入了圣克拉拉-->名单:

  • 圣克拉拉
  • 圣克拉拉

第三,我增加了圣塔莫妮卡->名单:

  • 圣莫尼卡
  • 圣莫尼卡
  • 圣莫尼卡

预期:

首先,我添加了:旧金山->列表:

  • 旧金山

第二,我加入了圣克拉拉-->名单:

  • 圣克拉拉
  • 旧金山

第三,我增加了圣塔莫妮卡->名单:

  • 圣莫尼卡
  • 圣克拉拉
  • 旧金山

http://plnkr.co/edit/iyJ1cFVizjcWbh3PCYBJ?p=preview

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-25 22:53:49

您正在为所有的{{result1}}元素显示<li>。将{{result1}}替换为{{x}}。在代码中也更改addItem()函数。将$scope.addMe替换为$scope.result1。对于要以相反顺序显示的列表,可以使用自定义筛选器。

代码语言:javascript
复制
<input type="text" class="form-control" id="Autocomplete" placeholder="Enter Airport Code or City Name" ng-autocomplete="result1" details="details1" options="options1" ng-model="addMe">
<button type="submit" class="btn btn-default" ng-click="addItem()">Add to Itinerary</button>

            <ul>
                <li ng-repeat="x in products | reverse">{{x}}</li> <!-- Replace result1 with x --> 
            </ul>


$scope.addItem = function () {
     $scope.products.push($scope.result1); // Replace $scope.addMe with $scope.result1
}

/* Custom Filter */
angular.module( "myApp").filter('reverse', function() {
   return function(items) {
     return items.slice().reverse();
   };
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39692521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档