使用angularjs无效的搜索建议功能

时间:2017-07-08 12:27:35

标签: angularjs json angularjs-scope angularjs-ng-repeat

我正在尝试在我的系统上运行以下代码,但无法获得结果。但是我正在运行的js fiddler上运行相同的代码。

这是我的index.html

<div ng-app="myApp" ng-controller="myCtrl">
<div ng-app>
    <div ng-controller="MyController">
        <input type="search" ng-model="search" placeholder="Search...">
        <ul>
            <li ng-repeat="name in names | filter:search">
                {{ name }}
            </li>
        </ul>
    </div>
</div>

的script.js

var app = angular.module('MyModule', []);

app.controller('DefaultCtrl', function($scope) {
    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];
});

app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});

另外我想得到exaxct匹配,如果我输入'A'只有以字母'A'开头的单词等等......

2 个答案:

答案 0 :(得分:1)

index.html

中替换它
<div ng-app="MyModule">
    <div ng-controller="DefaultCtrl">
        <input type="search" ng-model="search" placeholder="Search...">
        <ul>
            <li ng-repeat="name in names | filter:search">
                {{ name }}
            </li>
        </ul>
    </div>
</div>

答案 1 :(得分:0)

据我所知,您将控制器命名为 DefaultCtrl ,并在HTML中命名 你正在打电话

<div ng-controller="MyController">

因此改变它以匹配控制器名称,即

<div ng-controller="DefaultCtrl">