0

I have an input which uses angular to generate a list of names, and I want each name to be a link that submits the form with the name as the input value.

<form method="POST" action="/results/" id="artform>
{% csrf_token %}
   Search:<input type="text" name="search" ng-model="search" />
          <input type="submit" value="Submit" /> 
</form>
<table ng-controller="mycontroller">
    <tr ng-repeat="artist in artists | filter:search | limitTo:10">
        <td ng-hide="!search">
            <a class="click-me" href="javascript:$('#artform').submit();">
                 {({ artist })}
            </a>
        </td>
    </tr>
</table>

<script>
  (function ($){
    $(function () {
        $('body').delegate('.click-me', 'click', function (){
            $('input[name="search"]').val({({ artist })});
        });
    });
})(jQuery);    
</script>

The .val({({ artist })}); is where I want to inject whatever the value of {({ artist })} is, since I want the link to submit the form with the name as the value. How can I get the angular variable into the jquery?

4
  • 2
    That would be a terrible way to do this. Why not just make this form a part of your directive and handle the click from it? That would be the proper way to get it done. Commented Dec 1, 2014 at 7:33
  • I'm open to trying that, I haven't learned how. Maybe you could link me to something like it. Commented Dec 1, 2014 at 7:35
  • 1
    Let me whip up a fiddle for you Commented Dec 1, 2014 at 7:37
  • Well, it seems the docs had everything you needed, including a plnkr code demo. Find it at docs.angularjs.org/api/ng/filter/filter Commented Dec 1, 2014 at 7:48

3 Answers 3

1

try

$('body').delegate('.click-me', 'click', function (){
            var artist = $(this).text();
            $('input[name="search"]').val(artist);
 });
Sign up to request clarification or add additional context in comments.

Comments

1

There's no easy way to achieve that.

However, in your case, you can easily use:

$('input[name="search"]').val($(this).text());

Comments

1

You are combining two technologies that are not really compatible. It would be better if you choose to only use AngularJS in your site or only JQuery.

But to answer your question in jquery if you want the value to be the text in the anchor:

 $('body').delegate('.click-me', 'click', function (){
            $('input[name="search"]').val($(this).text());
        });

Alternatively if you would like to solve this problem in an Angular way do the following:

Create a method in your controller (mycontroller) called clickMe and bind the value of the anchor to the search textbox. You would have to move your controller up in your code so that the search box and the table is part of the same controller.

<div ng-controller="mycontroller">
<form method="POST" action="/results/" submit-on="clickMeEvent">
{% csrf_token %}
   Search:<input type="text" name="search" ng-model="search" />
          <input type="submit" value="Submit" /> 
</form>
<table >
    <tr ng-repeat="artist in artists | filter:search | limitTo:10">
        <td ng-hide="!search">
            <a ng-click="clickMe(artist)">
                 {({ artist })}
            </a>
        </td>
    </tr>
</table>
</div>

Controller:

  $scope.clickMe = function (artist) {
     $scope.search = artist;
     $scope.$broadcast('clickMeEvent');
};

Directive:

app.directive('submitOn', function() {
    return {
        link: function(scope, elm, attrs) {
            scope.$on(attrs.submitOn, function() {
                //We can't trigger submit immediately, or we get $digest already in progress error :-[ (because ng-submit does an $apply of its own)
                setTimeout(function() {
                    elm.trigger('submit');
                });
            });
        }
    };
});

4 Comments

Where in my controller do I inject that scope snippet?
Currently that pure angular code is not generating links.
Check out the controller documentation here: docs.angularjs.org/guide/controller. The "Simple Spicy Controller" Example has a controller method like the one defined above
Right you also want to submit the form... I missed that part. That is why the other guy suggested that you need a directive. Directives are used for accessing html code directly. So instead of going $('form').submit() like you might have done in jquery you now have to write a custom directive. See this example. jsfiddle.net/unWF3 I will edit the answer above just now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.