问题描述:

<a class="contacts" ng-click="contactSelect(contact.userID)">

<!---->

<span ng-switch-default="" my-user-status="::contact.userID">Hello World !</span>

<!---->

</div>

</a>

I'm using this code for click: $('.contacts').click(); and it is working, but my problem is I want to click on all tags a that have string Hello World !.

网友答案:

You should be able to do whatever it is you want to do not by focusing on the string inside the ng-switch element but by the property that changes what ng-switch displays and do everything inside your ng-click and get rid of using jQuery for this.

Change ng-click to pass in the whole contact object, not just the userID

<a class="contacts" ng-click="contactSelect(contact)">

Then in controller I will assume that the ng-switch is related to the contact object:

var switchValues =['one','two','three'];// if not in this array will use default
$scope.contactSelect = function(contact){
   var userId = contact.userId;
   // do something with the userId

   var switchValue = contact.switchProperty;
   // do something with the switchProperty
    if(switchValues.indexOf(switchValue ) === -1){
       // this will be default case with string "hello world" in the dom
       // do whatever you would have done in the jQuery
    }

}

This is very rough since not much detail was provided in the question

网友答案:
$(".contacts").click(function(e){
    if($(this).find("span").text() == "Hello World !"){
        e.preventDefault();
        // Do what you need to do here.
    } else {
        // String didn't match.
    }
});

or as user @Jon Uleis reference, you can use jQuery's ":contains()" selector like this:

$(".contacts:contains('Hello World')").click(function(e){
        e.preventDefault();
        // Do what you need to do here.
});
相关阅读:
Top