【问题标题】:AngularJS ng-repeat data in directive from parent?AngularJS ng-repeat 父指令中的重复数据?
【发布时间】:2015-05-14 02:58:20
【问题描述】:

我正在尝试从表单元素中获取数据,然后使用 jQuery .after() 插入一个以显示可能的选择。我的直接通过获取使用 AngularJS 加载的名为“数据”的属性从表单中获取可能的匹配项。现在的问题是,当我尝试使用新插入的 ng-repeat 时,它没有列出它。

'use strict';

angular
    .module('autoComplete', [])
    .directive('autoComplete', autoComplete);

function autoComplete($parse)  {
    var directive = {
        restrict: 'EA',
        require: '?ngModel',
        link: link,
    };

    return directive;

    function link(scope, elem, attr, ngModel) {
        var modelGet = $parse(attr['ngModel']);
        var modelSet = modelGet.assign;
        scope.$watch(function() {
            return modelGet(scope);
        }, function() {
            var string = modelGet(scope);
            if (string != undefined && string.length > 6) {
                var vm = this;
                vm.data = attr.data;
                vm.width = elem.outerWidth();
                elem.after('<div><ul class="list-group" style="position: absolute; width: '+vm.width+'px;"><li class="list-group-item" ng-repeat="codes in vm.data track by $index">{{code}}</li></ul></div>');

                console.log(vm.data); //This outputs the data just fine
            }

        });
    }
}

一旦放入,屏幕上产生的唯一东西就是{{code}}。我假设我应该尝试以某种角度编译它?

【问题讨论】:

  • 是的,你需要编译它才能工作,但为什么你在 Jquery 中这样做,而不是在更常规的 angular-y 方式?
  • 知道最好的方法是什么吗?
  • 我会说最小化JQuery,你能创造一个你现在拥有的小提琴吗?这样做会容易得多。

标签: javascript html angularjs angularjs-directive angularjs-ng-repeat


【解决方案1】:

您可以尝试在elem.after(... 之后添加$scope.apply(),以帮助消化$scope.watch() 中所做的更改。

参考:https://stackoverflow.com/a/15113029/2242217

【讨论】:

  • 这是一个问题还是一个答案?如果您需要澄清某些事情,请使用 cmets
  • 不需要澄清,这是修辞。答案已更新。
猜你喜欢
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2015-11-06
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
相关资源
最近更新 更多