1

I have working to pass a value from Jquery to angular js. But I have found undefined in angular js. My code :

HTML :
<button onclick="change('5')"></button>
<input type="hidden" ng-model="data" id="one" />

<button ng-click="test(data)">Test</button>

JQuery:
function change(data)
{
  $("#one").val(data).change();
}

Angular Js :

$scope.test= function(data)
{
   console.log(data)
}

But I got undefined. How can I do this ?

2
  • use ng-click like, <button ng-click="change('5')"></button> Commented Jul 30, 2015 at 6:16
  • actually jquery lite is already built in angular... So try doing everything angular way if you are using angular Commented Jul 30, 2015 at 6:32

1 Answer 1

2

You can do it for sure*, however you should probably not, as this blend of jQuery and Angular will soon become hard to maintain. This is not really well-design.

Instead, use angular directives and methods, in your case it's straightforward:

<button ng-click="change('5')"></button>
<input type="hidden" ng-model="data" id="one" />

<button ng-click="test(data)">Test</button>

and in Angular controller:

$scope.change = function(x) {
    $scope.data = x;
};


* If you really want it: you need to let Angular know that you have changed the model from outside of digest cycle with manual $("#one").val(data).change(); angular.element($("#one")).scope().$apply();.

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

1 Comment

Where I will use angular.element($("#one")).scope().$apply(); ? In Jquery or Angular Js

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.