2014-01-16 49 views
2

我有以下HTML:Angularjs指令 - 一類名來選擇下一個元素

<div> 
    <input type="text" ng-model="search.q" special-input> 
    <ul class="hidden">...</ul> 
    <ul class="hidden bonus">...</ul> 
</div> 

與以下指令:

myApp.directive('specialInput', ['$timeout', function($timeout) 
{ 
    return { 
    link: function(scope, element) { 
     element.bind('focus', function() { 
     $timeout(function() { 
      // Select ul with class bonus 
      element.parent().find('.bonus').removeClass('hidden'); 
     }); 
     }); 
    } 
    } 
}]); 

我要選擇使用jqLit​​e的ul.bonus但不能想辦法。我嘗試使用.next(「。bonus」),但選擇器完全被忽略,並且選擇了第一個ul。有沒有人有一個想法,爲什麼我不能這樣做?

P.S.我只是依靠沒有jQuery的AngularJS內部jqLit​​e。

謝謝!

回答

2

你不會需要一個指令來實現這一目標:

<div> 
    <input type="text" 
      ng-model="search.q" 
      ng-focus="bonus=true" 
      ng-blur="bonus=false"> 
    <ul class="hidden">...</ul> 
    <ul ng-show="bonus">...</ul> 
</div> 

,如果你的需求是更復雜的把有關bonus狀態決定在您的控制器。

+0

謝謝,你說得對,這種方法好得多 – Angelin

+0

嗨,但實際上如何使用jqlite按類選擇下一個元素?在我的情況下,我需要做,但不知道如何。到目前爲止,我沒有'link.parent()。next()。toggleClass('active');'這當然不能按預期工作。 jQuery相當於'link.parent()。siblings('.active')。toggleClass('active');'。感謝您的未來答案 – lkartono