json - ng-tags-input 自动完成功能不显示

标签 json angularjs autocomplete ng-tags-input

我尝试将 ng-tags-input 与 api Controller .net Mvc 6 返回的 Json 列表一起使用。我的列表是在 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
 }]

这是我 Controller 中的方法:

 $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 Controller :

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

Angular 商店Ctrl:

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 员工Ctrl:

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

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

最佳答案

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

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并使用自定义对象查看输入的标签。

关于json - ng-tags-input 自动完成功能不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783027/

相关文章:

javascript - 使用 jquery 在加载页面上的 strust2 中自动完成

node.js - browserify后客户端 Node 模块不起作用

java - 解析 json 时 GSON 中的数字格式异常

json - 向 Golang API 发送请求以发送 JSON 和字符串化 JSON

javascript - ui-mask 的 Angular js 指令的单元测试

javascript - Http post没有返回响应并且Angular js中的请求没有错误?

javascript - django 无法将 dictdata 传递到 javascript 变量中

javascript - 在 couchBase 中创建 View 时如何将输入变量设置为 json 中的键

javascript - 如何计算嵌套JSON的长度?

vim - vim 中的类似 Bash 的代码完成?