Reputation: 91
create{
var view;
$(".l_item").click(function(){
view = this.value;
});
console.log(this.view);
}
This is my jquery function used to get the selected value of the I_item (array of list).after getting this value i need to use this value outside the function
But i get undefined value..so how to achieve this ? any help will be appreciated,
Upvotes: 0
Views: 220
Reputation: 27303
Define Your Variable Outside click function then use that variable inside your test() method
$scope.view;
$(".l_item").click(function(){
$scope.view = this.value;
});
Then you can access the view variable outside the click function but it will work
test(){
console.log($scope.view);
}
Upvotes: 1
Reputation: 360
You haven't mentioned here the Angular Version which you are using here. So, I am providing you the logic in context to the angular 2. Please follow below steps:-
1) If the jquery is installed then write following piece of code to access jquery inside the component.
declare var $: any;
2) It is always a good practice to write all the jquery in some external JS file, instead of writing at component level. So I am assuming that you already have an external JS file which has a function test() inside it.
3) Now at component level, in order to access that function you need to write the following line of code
declare var test: any;
4) Now in case you want that test() function should return some value, then inside the JS file you need to return that value as follows:-
test(){
return "some value";
}
5) You can get the value from jquery function at component with the following line of code :-
var returnValue=test();
6) Do remember that your external js file should load after the main jquery file. You can add external jquery file to the angular module by declaring it inside angularCLI.json as follows:-
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../src/assets/js/core/testmodule/customtest.js"
],
All the best.
Upvotes: 0