0

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.

1
  • 1
    why do you need jQuery to do this? Show some code Commented Feb 25, 2015 at 16:50

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @simon, this passes to the 001 $rootScope, there is a nested 002 frontEnd scope also, it needs to be passed into that.
Bit hard to figure out without seeing your code. Can you post a bare bones version?
0
window.property = $('#input_3').val();

$scope.input3val = window.property;

9 Comments

why do you need to pollute global namespace to do this?
if you use jquery with angular there is no way to pass 'variables' without polluting global namespace, or at least i don't know how
Please tell me how to do this without polluting global namespace or dom, i'm really interested. [i don't allow hidden inputs or other hacks like this as a response]
best way is run jQuery code inside a directive so you have scope access. Very hard to know what OP needs though without seeing any code
no idea how you got that all figured out from a one sentence question and no code
|
0

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.

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.