Bleistift
Bleistift

Reputation: 27

Assign jquery variable to a angularjs variable

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

Upvotes: 0

Views: 2692

Answers (2)

Igor Monteiro
Igor Monteiro

Reputation: 953

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';
    });
});

Upvotes: 0

DonJuwe
DonJuwe

Reputation: 4563

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.

Upvotes: 1

Related Questions