下拉不工作AngularJs

时间:2016-06-06 13:16:56

标签: javascript html angularjs

  

列出dropDown时遇到问题。我看看Chrome检查看起来数据是否存在但由于某种原因,当我点击它时,下拉列表不会显示。   我从BooksController中提取数据并加载所有选项,但是没有显示列表,它可能是html上的东西,但我无法实现。



var app = angular.module('MyApp', [])
    .controller('BooksController', function($scope, LocationService) {
        $scope.categories = [
        {
            "Id": "-1",
            "Name": "All Books"
        }, {
            "Id": "0",
            "Name": "Computer Science"
        },{ 
            "Id": "1",
            "Name": "Physics"
        },{ 
            "Id": "2",
            "Name": "History"
        },{ 
            "Id": "3",
            "Name": "Geography"
        }, {
            "Id": "4",
            "Name": "Fiction"
        }];
        $scope.categoryId = null;
        $scope.BooksList = null;

        LocationService.GetBooks().then(function(d) {
            $scope.BooksList = d.data;
        });
        $scope.GetBookByCategory = function() {
            $scope.BooksList = null;
            LocationService.GetBooksFiltered($scope.categoryId).then(function(d) {
                $scope.BooksList = d.data;
            }, function(error) {
                alert('Error!');
            });
        }
    })
.factory('LocationService', function ($http) {
        var fac = {};
fac.GetBooksFiltered = function(categoryId){
    return $http.get('/Home/Get?predicate=' + categoryId)
}

fac.GetBooks = function(){
    return $http.get('/Home/GetAll')
}

        return fac;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="pull-right">
        <div class="dropdown">
            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
                Filter By: {{item.Name}}
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
                <li data-ng-repeat="item in categories" dataitem-id="{{item.Id}}"><a     href="#">{{item.Name}}</a></li>
            </ul>
        </div>
    </div>
&#13;
&#13;
&#13;

强文

2 个答案:

答案 0 :(得分:0)

您必须将您的BooksController添加到HTML代码中,就像有人在评论中说的那样。

只需在.pull-right div标记中添加:

ng-controller="BooksController"

此外,您应该在另一个html标记上具有ng-app="MyApp"属性,该标记是.pull-right div标记的父级,以便使其正常工作。

答案 1 :(得分:0)

我认为你不在你的HTML代码中使用ng-app和ng-controller 看到这里现在正在运作。

 <body ng-app="myApp" ng-controller="MyCtrl">
<h1>Hello Plunker!</h1>
{{test}}
<ul class="dropdown-menu">
    <li data-ng-repeat="item in categories" dataitem-id="{{item.Id}}"><a     href="#">{{item.Name}}</a></li>
</ul>

Plunker