0

How can i assign a jquery element to angularjs? i tried:

var scope = $scope;
scope.project = $( "#project" ).val();

But it didn't worked for me. Someone has a solution for this?

Thanks in advance

4
  • 1
    I dont find any problem in it.What error have you got ? Commented Jan 30, 2015 at 11:40
  • 2
    You should never run your jquery code inside your controller.Rather than trying to make this work, try to find out how to avoid this. Commented Jan 30, 2015 at 11:40
  • Why do you have to do this at all? Can't you add ng-model="project" to the input field? Commented Jan 30, 2015 at 11:41
  • those input field i generate dynamically. Each field has a id. For this example, the field Project has the id project. I need that variable for my request, which i send to my server. So i need that variable. I don't like to use $( "#project" ).val(); Commented Jan 30, 2015 at 11:45

2 Answers 2

1

Bind a model via ng-model attribute to your input and access it whenever you want to use it:

Fiddle

Moreover, as @Abhishek Jain pointed out, you should never use jQuery code within your controller. You could use a directive for that.

Sign up to request clarification or add additional context in comments.

4 Comments

i also tried $scope.project = $("#project").val(); but didn't work. But how do i get those value without using jquery code?
please provide some more of your code, especially when you want to get the value. fiddle would be nice.
jsfiddle.net/cty7nta1/1 I'm just testing, what's possible with jquery and angularjs
Well, normally you would use ng-model at this input. When you do your ajax call you can access all those scope values by their model. See my updated answer.
0

I guess that is the best way:

put inside your jquery event..

$(document).ready(function () {
    var controllerElement = document.querySelector('[ng-controller="yourController"]');
    var scope = angular.element(controllerElement).scope();
    scope.$apply(function () {
        scope.yourProperty = 'your value';
    });
});

Comments

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.