Angular does not recognize a variable out of control

Asked

Viewed 575 times

0

I have a question, I have searched in other forums and I did not find the solution. I did a test on a code below just for demonstration.

*//dentro do controle do angular*
angular.module("NaBalada").controller("NaBaladaLocal", function(data){
    return $scope.teste = 'Testando Angular';
    console.log(testejs)
});
*//Script normal JS*
console.log(teste);*//teste do $scope.teste*
let testejs = 'Variável fora do angular, pra reconhecer dentro do controller'

then my problem is that make the variables be seen inside and outside, so you can receive some value in my application, the error that appears is 'test is not defined' and 'testejs is not defined'

Someone can help me, from now on, thank you!

  • Why do you want to use it outside? I believe that they worked very well inside the controller, in addition if you were to use some element outside the scope 'Nabaladalocal', you would have to declare another controller and share information between them.

  • I’m using a library to generate polygons and I had to do this outside the angle, and to insert it into my database using the angular $http.post.. but for this I have to make the angular recognize the variaces that are in my script js.

  • I get it, you’re using a global variable which is very dangerous kk, I recommend you read this https://answall.com/questions/32251/vari%C3%A1vel-global-in-javascript

  • So I took a look, but I still couldn’t accomplish what I wanted. I think the best way for me to try to clarify how it works would be, where you have //normal JS script; compare it as a second file. the first part teste1.js and the second part teste.js who carry in the same index.html. but when I declare for example var tst2 = "Teste" when I say console.log(tst2) inside my angular controller it does not recognize. and gives what I have already reported in the question. :/

2 answers

1

In your code, you are interrupting the execution of the function before to print the contents:

angular.module("NaBalada").controller("NaBaladaLocal", function(data){
    return $scope.teste = 'Testando Angular';
    console.log(testejs)
});

Nothing is processed after the keyword return, that ends the execution of the function. Try the following change:

angular.module("NaBalada").controller("NaBaladaLocal", function(data){
    console.log(testejs);
    $scope.teste = 'Testando Angular';
});

0

I managed to solve in a way, I still do not know if it is the correct way just for me to capture a geojson and play in my database, used as follows and worked.

*//dentro do controle do angular*
angular.module("NaBalada").controller("NaBaladaLocal", function(){
    window.teste = 'Testando Angular';
    console.log(testejs);
});

*//Script normal JS*
console.log(teste);*//teste do $scope.teste*
window.testejs = 'Variável fora do angular, pra reconhecer dentro do controller'

I’ll leave it there, it might help someone.

  • See Onosendai’s answer, it will guide you better about the problem, what caused it and how to solve it.

  • Thank you, if you can read in the above comments I made with Felipe Duarte you can understand better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.