If the function is defined as

function myFunction(){...} 

then on the iPhone Chrom gives an error. What - I do not know, because. There is no possibility to look at the console. But the execution of AngularJS stops. If the function is defined as

 var myFunction = function(){...} 

That's all fine.

Has anyone encountered this problem and how can it be circumvented, otherwise there are a lot of definitions of the first type in the code?

More code in the question: Minimum working code:

 var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.newVar = "Its not work"; $scope.myFunction = function() { return $scope.newVar = 'Its work'; }; $scope.myFunction(); }); 
 <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="personCtrl"> <span> Angular <span ng-if="0">не </span> работает </span> <br /> <span ng-bind="newVar"> Its not work </span> </div> </body> </html> 
This code will work on all devices. The line "Angular works" - demonstrates that the angular has basically loaded. The line "Its work" demonstrates that the controller has worked correctly.

And this code:

 var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.newVar = "Its not work"; $scope.myFunction = myFunction; function myFunction() { return $scope.newVar = 'Its work'; }; $scope.myFunction(); }); 
 <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="personCtrl"> <span> Angular <span ng-if="0">не </span> работает </span> <br /> <span ng-bind="newVar"> Its not work </span> </div> </body> </html> 

It works on all devices except the iPhone (I can’t check it out right now, I’ll check it on Monday and make corrections)

Roman: You are right, and thank you for adding the code I have shortened (angular etc subconnect). What you wrote will work, incl. on the iPhone. But the style you applied in this place:

 $scope.myFunction = function() { return $scope.newVar = 'Its work'; }; 

It is considered not the best (as it seemed to me). It is usually recommended to do so (long look for the link):

 $scope.myFunction = myFunction; function myFunction() { return $scope.newVar = 'Its work'; }; 

But, suddenly it turned out that on the iPhone it does not work. The question is how to get around this so as not to rewrite all the code?

  • add more code to the question, according to the available information, nothing can be said - Grundy
  • one
    Oh yeah! Now everything is clear! - Grundy
  • try to remove "use strict" - Grundy
  • In addition, as can be seen from the example , the function declaration also works. The problem lies precisely in where and how the announcement takes place, and without providing a minimal reproducible example of reproducing an error, one cannot say exactly where to fix it . Grundy
  • Thank you for the idea with 'use strict'. I'll try on Monday. I can not, because I do not use iPhone. Corrected the issue. Where there used to be a shortened code, I inserted the "Minimum working code". (I also can’t check it on iPhone yet) I used the Roman code with a few amendments for this. Do not minus him much, he sincerely tried to help. - Dolalex

2 answers 2

The @Grundy sentence was removed

 'use strict'; 

True, for some reason, the minimal example, as it turned out, works with 'use strict', but the main site showed a clear dependence on this option.

The decision on specific actions is this: To finish the code with 'use strict', and then delete it everywhere, so that it also works on the iPhone.

But really nobody faced it !!!

@Grundy is right:

SCRIPT1047. In strict mode, function declarations cannot be located inside other operators or blocks. They can be located only on the top level or directly inside the body of another function.

  • The problem is that use strict imposes restrictions on where functions can be declared, for example, you cannot declare a function inside if : if(condition){ function func(){...}} , but some browsers swallow such descriptions, and some not. - Grundy
  • Therefore, instead of removing use strict you can check where and how non-working functions are declared. The problem in them is Grundy
  • In my live example, functions were declared in a try {} block at the very beginning of the script, even before the angular was launched, but after loading the angular itself and all the libraries I needed. Angular and libraries by themselves do not cause errors, it is verified. Then comes the definition of some variables and functions (here is the error), then the definition of the angular modules, then the config, run and bootstrap. At this stage, all of the above is caught. But, if you download subsequent scripts, the error reappears. After completion of the whole code I will dig further. - Dolalex
  • make a declaration of functions from the blocks, the functions must be located either without nesting somewhere. either directly inside another function, the bases of any other blocks - Grundy

I'm afraid to ask, but they really do it, I just started learning angular, so I'm not sure what's right, but in any case, I would write your example in some way

 <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="personCtrl"> <button ng-click="myFunction()">Click me!</button> <span ng-bind="newVar"></span> </div> <script> var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.newVar = "Its not work"; $scope.myFunction = function() { return $scope.newVar = 'Its work'; }; }); </script> </body> </html> 

  • This is an answer box. When your rating is sufficient, you can leave comments to the questions. - Denis
  • Denis, I know that this is an answer field, it is obvious that the answers write an answer in the field. I don't care about the rating and comments on the questions, I just wrote how I would do it - Roman
  • I honestly do not understand what is wrong with my answer, someone can clearly explain. In the question of a person, the binding does not work, most likely due to the fact that the variable is not recorded in a scop as a property and the function that changes it is also not recorded in a scop as a method - Roman
  • @pavel why not contain something? Looks like an honest attempt to resolve. - Nick Volynkin
  • @ Novel there the author of the question answered you right in the question. This may shed light on the possible shortcomings of the answer. - Nick Volynkin