Testing AngularJS I found a situation that I would like to know why it happens.
Take a look at the object $scope.film
that is defined in the controller below:
/* Modelo */
let app = angular.module('myApp', []);
/* Controlador */
app.controller('mainCtrl', ['$scope', function($scope){
/* Film */
$scope.film = {
titulo: 'Mad Max Fury Road',
año: 2015
};
}]);
As is, it is an object with a property titulo
and a property año
. In the case of the property año
, it is defined with the letter ñ .
$scope.film = {
titulo: 'Mad Max Fury Road',
año: 2015
};
If I want to use an expression in a view to print the value of the año
object's property, using the syntax objeto.propiedad
...
<!-- Vista -->
<div ng-app="myApp">
<div ng-controller="mainCtrl">
<p>{{film.año}}</p>
</div>
</div>
...I get an error:
Lexer Error: Unexpected next character at columns 6-6 [ñ] in expression [film.año].
Giving me to understand, according to the AngularJS error reference documentation, that the ñ character generates a lexical error in the expression:
Occurs when an expression has a lexical error, for example a malformed number (0.5e-) or an invalid unicode escape.
If, on the other hand, I use the same expression to print the value of the property año
using the syntax objeto['propiedad']
, it doesn't cause any problems, and the value of that property is shown to me in the view.
<!-- Vista -->
<div ng-app="myApp">
<div ng-controller="mainCtrl">
<p>{{film['año']}}</p> <!-- HTML: 2015 -->
</div>
</div>
test snippet
Here you can try it. I commented out the expression that does not give errors.
/* Modelo */
let app = angular.module('myApp', []);
/* Controlador */
app.controller('mainCtrl', ['$scope', function($scope){
$scope.film = {
title: 'Mad Max Fury Road',
año: 2015
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<!-- Vista -->
<div ng-app="myApp">
<div ng-controller="mainCtrl">
<p>{{film.año}}</p>
<!-- <p>{{film['año']}}</p> -->
</div>
</div>
I could just as well use the syntax objeto['propiedad']
to print my property value, or change the property name from año
to annio
(or others).
But the point of my question is to know why AngularJS does not allow the use of the letter ñ as part of a property name, using the syntax objeto.propiedad
inside an expression.