【问题标题】:Rebind listnav plugin using AngularJS使用 AngularJS 重新绑定 listnav 插件
【发布时间】:2015-08-18 02:11:35
【问题描述】:

我正在使用 listnav 插件jQuery listnav 来过滤我的数据结果。我创建了一个指令来调用 listnav() 方法。

当我再次将 ng-repeat 与新结果(示例中的字母 A)绑定时,插件会创建另一个导航栏。

请看一下我在http://codepen.io/anon/pen/XbwVWZ?editors=101中的示例

<div style="height: 50px"></div>  

<div ng-app="myApp" ng-controller="MyCtrl">        
    <input type="button" ng-click="send()" value="Load letter A"></input>
    <hr>    
    <ul id="demoOne" class="demo" listnav>
        <li ng-repeat="i in items" ng-bind="i.name"></li>
    </ul>

</div>

$(function(){
//$('#demoOne').listnav();

$('.demo a').click(function(e) {
    e.preventDefault();
});
});

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

myApp.controller('MyCtrl', MyCtrl);

function MyCtrl($scope) {
    var m = [
        {name: "411 Services"},
        {name: "Accountants"},
        {name: "Acupuncture"},
        {name: "Human Resource Consultants"},
        {name: "Importers"},
        {name: "Industrial - Equipment &amp; Supplies (Wholesale)"},
        {name: "Investments"},
        {name: "Janitor Service"},
        {name: "Jewelers (Wholesale)"},
        {name: "Jewelers - Retail"},
        {name: "Restaurants - Barbecue"},
        {name: "Roofing - Materials"},
        {name: "Roofing - Service Consultants"},
        {name: "Sand &amp; Gravel (Wholesale)"},
        {name: "School Supplies (Wholesale)"},
        {name: "Schools &amp; Educational Services"},
        {name: "Schools - Nursery &amp; Kindergarten Academic"},
        {name: "Vacuum Cleaners - Household - Dealers"},
        {name: "Vending Machines"},
        {name: "Ventilating Systems - Cleaning"},
        {name: "Wallpapers &amp; Wallcoverings - Installation"},
        {name: "Yoga Instruction"},
        {name: "Youth Organizations &amp; Centers"},
        {name: "Zilch"}
    ];

    $scope.items = m;

    $scope.send = function(){
        var n = [            
            {name: "Accountants"},
            {name: "Acupuncture"}
        ];            

        $scope.items = n;
        $scope.loadNav();
    };   
}

myApp.directive('listnav', function($timeout){
    return {
        restrict: 'A',
        replace: false,
        link: function($scope, elem, attrs){            

            $scope.loadNav = function(){
                $timeout(function(){
                    $(elem).listnav();
                });
            };

            $scope.loadNav();
        }
    };
});

【问题讨论】:

    标签: jquery angularjs angularjs-directive angularjs-ng-repeat


    【解决方案1】:

    您在调用send() 时再次调用了loadNav() 函数。

    $scope.send = function(){
        var n = [            
            {name: "Accountants"},
            {name: "Acupuncture"}
        ];            
    
        $scope.items = n;
        //$scope.loadNav(); // this change
    };  
    

    Codepen update

    根据评论编辑

    你可以删除前一个;

    $scope.send = function(){
        var n = [            
            {name: "Accountants"},
            {name: "Acupuncture"}
        ];            
    
        $scope.items = n;
        $(".listNav").remove(); // added this
        $scope.loadNav();
    };  
    

    Codepen update

    【讨论】:

    • 这样,条形图不会更新,并且一直显示错误的计数和高亮字母。
    • 快到了!字母 N 计数“没有匹配的条目”,并且选项“全部”增加每次点击。我正在寻找修复方法。感谢 .remove() 的想法。
    • 我修复了它,暂时更改了默认选项 noMatchText: ''
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2017-05-20
    • 2015-06-07
    相关资源
    最近更新 更多