-5

I have an Angular js view.html page where I am passing a string called datadict from the $scope method of the controller.
Value {{ datadict.days }} is displaying value Nearly everyday and {{ datadict.days.includes('Nearly everyday') }} is showing result true.

Here is my code :

 <div class="some_class">
  <ul class="ex1">
    <li><a ng-click ="datadict.days.includes('Not applicable')" class="mdcl-tab1">Not applicable</a> </li>
    <li><a ng-click ="datadict.days.includes('Several Days')" class="mdcl-tab1">Several Days</a> </li>
    <li><a ng-click ="datadict.days.includes('More than half the days')" class="mdcl-tab1">More than half the days</a> </li>
    <li><a ng-click ="datadict.days.includes('Nearly everyday')" class="mdcl-tab1">Nearly everyday</a> </li>
  </ul>
</div>

And when an element gets clicked, the event is handled by this function :

<script>
$(document).ready(function () {

    $(".mdcl-tab1").on('click',function(){

        $(this).addClass('tick');
        $('.ex1').attr('id', 'ex1');

    });

});

</script>

The problem is I want to trigger the click event on that <a> element for which the above condition is true that's why I added this condition ng-click ="datadict.days.includes('Nearly everyday')" but I am not getting the desired result. What am I doing wrong ?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
  • but title of question is saying something else – Jigar7521 Nov 10 '16 at 07:31
  • What are you trying to achieve? You want to trigger the "click" event when the condition becomes true? Or do you want to enable the "click" event in that case? – KWeiss Nov 10 '16 at 07:34
  • what do yo mean by trigger the click event on condition true? do you mean trigger as in element.click() or do you want execute some method when the condition is true? – Sreekanth Nov 10 '16 at 07:52
  • does these anchor tags have a click handler bound else where? they are definitely not bound in angular, I suppose. Its really hard to solve the problem, when we dont understand the context. – Sreekanth Nov 10 '16 at 08:26
  • Possible duplicate of [How to trigger ng-click \[AngularJS\] programmatically](http://stackoverflow.com/questions/22447374/how-to-trigger-ng-click-angularjs-programmatically) – Daedalus Nov 18 '16 at 06:42

2 Answers2

3

You can do something like this,

<script>
$(document).ready(function () {
$(".mdcl-tab1").on('click',function(){

    $(this).addClass('tick');
    $('.ex1').attr('id', 'ex1');

});

if($scope.datadict.days.includes('Nearly everyday')){
$(".mdcl-tab1").trigger("click");
}
});

and remove the ng-click attribute from the anchor tag

<a class="mdcl-tab1">
GraveyardQueen
  • 771
  • 1
  • 7
  • 17
2

i think you can do something like this

<li><a ng-click ="datadict.days == 'Nearly Everyday' && someFunction()" class="mdcl-tab1">Not applicable</a> </li>

then if you click that ,someFunction() will only run if datadict.days is equal to 'Nearly Everyday'