What causes the syntax error when attempting to delete using ng-init in a straightforward manner?

Take a look at this short code snippet (jsfiddle):

<div ng-app="App" ng-init="foo={a: 20}; delete foo['a']">
</div>

This causes a syntax error in both Firefox and Chrome browsers. But why?

The syntax error states, "Token 'foo' is an unexpected token at column 21 of the expression [foo={a: 20}; delete foo['a']] starting at [foo['a']]."

Interestingly, using delete foo['a'] or delete foo.a results in functionality in pure JavaScript but not when introduced within the ng-init directive.

Answer №1

According to the ngInit documentation, it requires an expression that is a specialized subset of JavaScript recognized by AngularJS.

Expressions are snippets of code that resemble JavaScript and are commonly used in bindings like {{ expression }}. These expressions get processed by the $parse service and are often transformed using filters for better user readability.

Although Angular view expressions may seem similar to JavaScript expressions, it's important to note that Angular doesn't utilize JavaScript's eval() function for evaluating them.

(emphasis added)

Therefore, the delete operation is not compatible with $parse.

The documentation for ngInit also states:

The primary purpose of ngInit is to create aliases for special properties within ngRepeat, as demonstrated below. Apart from this specific scenario, it is recommended to use controllers instead of ngInit for initializing scope values.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to conceal the prior window prior to revealing the subsequent one

Currently, I am utilizing the project found at http://angular-google-maps.org/#! to integrate with AngularJS. According to the documentation available at , it is recommended to use a window directive for displaying information. I have implemented the win ...

What is the method for displaying script commands within package.json files?

With a multitude of repositories, each one unique in its setup, I find myself constantly referencing the package.json file to double-check the scripts. "scripts": { "start": "npm run dev" "build:dev": "N ...

Ways to conceal an element in Angular based on the truth of one of two conditions

Is there a way to hide an element in Angular if a specific condition is true? I attempted using *ngIf="productID == category.Lane || productID == category.Val", but it did not work as expected. <label>ProductID</label> <ng-select ...

How to retrieve a value from ng-options using AngularJS?

I want to display a dropdown in which users can select a specific month. Currently, the dropdown is populated with all 12 months in an array. How can I make sure that only the selected month is fetched from the dropdown? Code Snippet: $scope.Month = [&ap ...

Tips for managing a PHP post request without full knowledge of the variables to retrieve

My form allows users to dynamically add new lines using JavaScript. However, when they click the save button, I am struggling to capture and assign the new data to a variable. The current issue is that once the user adds new rows and clicks save, the rows ...

What is the reason that self focus doesn't function in JavaScript?

Whenever an input element triggers a blur event, I want to focus on a specific element. The issue arises when I try to focus on the same element that caused the blur event. Why does this only work when the element I am focusing on is not the one triggeri ...

How to Generate an Array of JSON Objects in JavaScript on a Razor Page using a Custom ViewModel in MVC?

Attempting to populate the array within my script for future charting with D3.JS, I came across some issues. Following advice from this post, I used a specific syntax that unfortunately resulted in an error stating "Uncaught ReferenceError: WebSite is not ...

"422 (Unprocessable Entity) Error When Submitting a Form in Rails Application

I recently delved into the world of ruby on rails a few days back. My current challenge involves transferring data from html tags to a ruby function using ajax. Below is the error message that has been giving me trouble: POST http://localhost:3000/ajax/o ...

How can I handle errors in the .then method with Angular's $http?

I am attempting to display a specific message in case of a server error or similar issue using Angular: $http.post(url,data).then(function(data) {//code if successful}); However, the response always shows as 200 even when intentionally introducing an e ...

Transform JSON-serialized string with HTML entities into an object

Looking for a solution to convert the following string into an object using Javascript: "[&quot;Software&quot;,&quot;3rd Party&quot;]" While I know how to convert HTML Entities to DOM Objects with this code: $("<div/>").html(encode ...

Expanding and collapsing multiple tables in Material-UI

I'm currently working on creating a collapsible table using MaterialUI. At the moment, all my slides have collapses but they are connected to one state for "open", so when I open one slide, all the other slides also open. Here is an example sandbox t ...

Angular Material Sidenav fails to cover the entire screen while scrolling

https://i.stack.imgur.com/32kfE.png When scrolling, the Sidenav is not expanding to take up 100% of the screen and it continues to scroll along with the page content. <div layout="column"> <section layout="row" flex> <!-- siden ...

I am trying to access the serial number data from an array of objects in ReactJS. Can anyone guide me

Is there a way to extract data from an array of objects, such as getting the serial number in ReactJS? In my current code snippet, I have an array called "number" with values [1, 2, 3]. My goal is to retrieve and display these numbers as a string like th ...

What is the best way to load an index.js file within a plugin framework?

I currently have an isolated index.js file located at plugins/some_plugin/index.js After attempting to run require(path_to_index.js) in my application, I encounter a 'cannot find modules' error message. This issue is understandable as the "some ...

What are some ways to optimize the efficiency of handling a sizable JSON object in React Native?

I am currently developing an application using React Native and have encountered significant slowdowns during transitions when loading more data. I am exploring alternative app structuring methods to prevent these performance issues, especially as the JSON ...

The favicon appears broken upon opening a new tab

Usually, I use a favicon PNG file for my website. It works perfectly and I can see my favicon on the browser tab. However, when I open a PDF document in a new page from my Angular app, I notice that there is a broken icon on the browser tab. Here is how I ...

"Create a Vue element containing a trio of buttons each displaying a distinct numeral

I am currently dealing with a component that generates three items (boxes) each containing three buttons (created using v-for). The issue I am facing is that when I click on a button, all the numbers inside the buttons change simultaneously. I want to be a ...

KineticJS: Applying a filter to an image does not result in the image having a stroke

Working with KineticJS version 5.1.0 I encountered an issue where a KineticJS image that had a stroke lost the stroke after applying a filter to it. I have created a demo showcasing this problem, which can be viewed on JSFiddle. Here is the code snippet: ...

What is a dynamic component in Vue with Typescript?

I need help implementing type checking for my dynamic component instead of relying on 'any' as a workaround. Can someone guide me through the proper way to achieve this? <script> ... interface { [key: string]: any } const pages: page = ...

Background of jQuery-UI Slider

Can the background color of a jQuery-UI Slider widget be set using JavaScript? This is the default setting: What I am trying to accomplish is the following: The green range should be determined based on historical data. I want to visually show the user ...