wbdlc
wbdlc

Reputation: 1089

How to pass a variable from jQuery to Angular property

I have a property inside an angular app called selected_tax, how can I pass a variable to this from jQuery?

I have a list of items that get filtered by their categories, I need a way to link to a pre-filtered list of category items. Currently, jQuery grabs a variable passed through the URL which is the category number.

Upvotes: 0

Views: 1098

Answers (3)

Steve Mitcham
Steve Mitcham

Reputation: 5313

The method $location.search() will get you access to the query string of the url as a JSON object. From the docs:

// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}

you can add $location to a controller through the dependencies

var app = angular.module("app",[])
app.controller("myController",["$scope, $location",function($scope, $location) {
   $scope.query = $location.search();
});

In your view template, you should then be able to use the query property to configure your jQuery calls.

Upvotes: 0

simon
simon

Reputation: 526

You can access the scope from the "outside world". Just make sure you wrap the code inside a setTimeout to allow angular to do its magic and then invoke digest.

<div ng-app="app" ng-controller="ctrl" id="myapp">{{xxx}}</div>
<script>
var app = angular.module("app",[])
app.controller("ctrl",["$scope","$log",function($scope,$log){
  $scope.xxx = 'hello';
}])
</script>

<script>
  setTimeout(function(){
    $("#myapp").scope().xxx='gotcha!!! HAHA!';
    $("#myapp").scope().$digest();
  },0)
</script>

Upvotes: 1

Razvan Dumitru
Razvan Dumitru

Reputation: 12452

window.property = $('#input_3').val();

$scope.input3val = window.property;

Upvotes: 0

Related Questions