简体   繁体   中英

How to pass a value from Jquery to angular Js?

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 ?

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(); $("#one").val(data).change(); angular.element($("#one")).scope().$apply(); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM