不显示ng-tags-input自动完成

时间:2015-09-25 13:12:01

标签: json angularjs autocomplete ng-tags-input

我尝试使用带有返回的Json列表的ng-tags-input由api控制器.net Mvc 6.我的列表是在json中创建的,但是当尝试使用自动完成显示此列表时,没有任何作用。我的自动填充列表未显示,我在chrome控制台中没有错误。

所以这是我列表中的一个对象:

[{
  "ShopID":1,
  "CompanyID":1,
  "RegionID":1,
  "Name":"Les Pieux",
  "Town":"Les Pieux",
  "Address":null,
  "ZipCode":null,
  "CreateDate":"2006-01-01T00:00:00",
  "ModificationDate":"2006-09-29T00:00:00",
  "LastModificationUserID":1,
  "PhoneNumber":null,
  "Fax":null,
  "Email":null,
  "CEmployeeShop":null
 }]

这是我控制器中的方法:

 $scope.tokenShops = [];
 $scope.loadJsonShops = function(query)
    {
        //$scope.shops contains my list of shops in json format.
        return $scope.shops;
    }

Html中的标签:

<div ng-controller="EmployeesCtrl">
            <tags-input ng-model="tokenShops"
                        display-property="Name"
                        Placeholder="Ajouter un magasin"
                        add-from-autocomplete-only="true">
                <auto-complete resource="loadJsonShops($query)"></auto-complete>
            </tags-input>
        </div>

这是填充$ scope.shops

的代码

Api控制器:

public IEnumerable<Shop> Get()
{
    using (LSContext db = new LSContext())
    {
        var listShop = db.Shops.ToList();
        return listShop;
    }
}

angular shopCtrl:

function ShopsCtrl($scope, $http, $rootScope) {
function getShopsList() {
    var reqGetShops = $http({ url: 'api/Shops' });
    reqGetShops.success(function (data) {
        $scope.shops = data;
        $rootScope.$broadcast("getListShops", { list: data });
    });
}
//with api controller the list is returned in json format. I tried an another method to fill my list with an convertion that I do and it doesn't work.

angularjs EmployeeCtrl:

$scope.$on("getListShops", function (event, args) {
    $scope.shops = args.list;
    $scope.selectShop = args.list[0];
})

但我不认为我的问题来自我的json列表。 我希望有一个人可以帮助我 。祝你有愉快的一天。

3 个答案:

答案 0 :(得分:2)

我用指令解决了我的问题:

angular.module('TagsDirective', ['myResources', 'resourcesManagerGet', 'translateI18n'])
.directive('tags', function ($http, $q) {
    return {
        restrict: 'E',//restraint pour les éléments du dom
        template: '<tags-input ng-model="SShops" key-property="ShopID" display-property="Name" placeholder="{{\'AddShop\' | i18n}}" add-from-autocomplete-only="true"><auto-complete source="loadTags($query)"></auto-complete></tags-input>',
        scope: false,

        link: function (scope, el) {
            scope.loadTags = function (query) {
                var deferred = $q.defer();
                var reqGetShops = $http({ url: 'api/Shops/GetListShopFiltered', params: { 'query': query } });
                reqGetShops.success(function (data) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            }
        }
    }
});

并在Html中:

<tags></tags>

g0loob:感谢您的帮助,但现在您可以放置​​一个对象数组并使用属性display-property来选择要显示的文本属性。

示例:http://mbenford.github.io/ngTagsInput/demos并查看使用自定义对象输入的标记。

答案 1 :(得分:0)

auto-complete要求对象数组至少包含一个名为“text”的属性(就像tags-input),如果您没有将模板用于auto-completetags-input 。您还需要过滤auto-complete的结果才能正常工作。另请参阅此link

答案 2 :(得分:0)

angular.module('TagsDirective', ['myResources', 'resourcesManagerGet', 'translateI18n'])
.directive('tags', function ($http, $q) {
    return {
        restrict: 'E',//restraint pour les éléments du dom
        template: '<tags-input ng-model="SShops" key-property="ShopID" display-property="Name" placeholder="{{\'AddShop\' | i18n}}" add-from-autocomplete-only="true"><auto-complete source="loadTags($query)"></auto-complete></tags-input>',
        scope: false,

        link: function (scope, el) {
            scope.loadTags = function (query) {
                var deferred = $q.defer();
                var reqGetShops = $http({ url: 'api/Shops/GetListShopFiltered', params: { 'query': query } });
                reqGetShops.success(function (data) {
                    deferred.resolve(data);
                });
                return deferred.promise;
            }
        }
    }