Can you delete angular attributes from HTML elements?

I delved into understanding the inner workings of Angular.js using a basic example.

<div ng-app="">
  <p>Type something in the input field:</p>
  <p>Name : <input type="text" ng-model="name" placeholder="Enter name here"></p>
  <h1>Hello {{name}}</h1>
</div>

https://i.sstatic.net/dQxji.gif

Is it possible to solely use Angular within an external JavaScript file, without any HTML connection?

In other words, can I achieve something like this...

document.getElementsByTagName('input')[0].ng-model = "name";
document.getElementsByTagName('h1')[0].innerHTML = "Hello {{name}}";

Do I have to integrate with HTML attributes or is it optional?

I couldn't discover any relevant information on this. Thank you for your help.

Answer №1

Similar to other MVVM frameworks, Angular is designed with an "obtrusive" approach, meaning it is integrated directly into the HTML rather than being used unobtrusively. To learn more about this concept and how it relates to Angular, check out this discussion on programmers.stackexchange.com: Is obtrusive JavaScript required to support a feature of AngularJS?

However, this doesn't mean that using Angular unobtrusively is impossible. It is still achievable by manually adding the necessary attributes and elements before initializing Angular on your document object model (DOM). This can be done through manual initialization instead of the default automatic initialization. Here's an example of how you could achieve this:

document.getElementsByTagName('input')[0].setAttribute("ng-model", "name");
document.getElementsByTagName('h1')[0].innerHTML = "Hello {{name}}";
// ...additional code here...
angular.bootstrap(document, ['myApp']);

It's important to note that while this approach is possible, it deviates from the intended usage of Angular.

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

What is the best way to manage communication with a database in a Node.js application?

I have a specific structure in my Express app: There is a db helper that I use to interact with my MariaDB database Here is the code snippet: var MariaSQL = require('mariasql'); var db = new MariaSQL(); var queries = { getUserByID : &a ...

Retrieve the data of elements that have been clicked using jQuery

I am struggling with a particular issue and would appreciate some assistance. Recently, I've been working on developing buttons that, when clicked, add data to a form for submission. An example of the code structure is as follows: <div> < ...

What is the procedure for incorporating JavaScript into my tic-tac-toe game's HTML and CSS?

I have created a simple Tic Tac Toe game using HTML and CSS. Although there is no embedded Javascript, I do reference a Javascript file for functionality. Can anyone provide me with the most straightforward Javascript code that will display an 'X&apos ...

Encountering a difficulty in sending data to the server through ajax with Cordova

I am currently working on a Phonegap Cordova project and I'm trying to send data to the server using AJAX but I'm encountering an error. Here is an example of my code: $(document).ready(function() { $('#frm').submit(function() ...

Failed to retrieve UI data from localstorage while performing an asynchronous action

Whenever I click on a specific "task," it loads correctly and the correct UI with data is displayed. The issue arises when clicking on the link through the browser input window - localhost:port/task/edit/id, or when manually refreshing the page for each "t ...

Selection dropdown not being populated by ng-options

After trying numerous examples to implement the changes, I am still facing issues. Even though I have an ng-model and clearly defined the object property I want to receive, it is not functioning as expected. Any assistance would be appreciated. Here is th ...

JavaScript running out of memory during yarn build process

While working with yarn to build my project, I encountered a problem. The issue at hand is that the memory heap exceeds its allocated size, causing the build process to fail. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - Java ...

Create new instances of Backbone Models using an existing Backbone Model as a template

Within my app, I am planning to include a configuration file called config.json as a Backbone Model. Here is an example of how it will be loaded: var Config = Backbone.Model.extend({ defaults: { base: '' }, url: 'config. ...

What's the better approach for creating Maps and Sets in ES6: relying on a compiler or leveraging syntactic sugar?

It has always baffled me why we are unable to write ES6 Maps in the following way: new Map({ ['ArrayAsKey']: {foo:bar} }) Is there a workaround for this limitation? Or perhaps a technique that enhances the experience of utilizing these mode ...

The onChange function is not triggered when the dropdown menu consists of only one item

I am facing an issue with my dynamically populated dropdown menu. When the dropdown contains only one element, the onChange() function of my select tag does not work as expected. It functions perfectly fine when there are multiple elements present. How c ...

Error encountered in Ubuntu while attempting to run a Python script within a Node.js/Express application: spawn EACCES

Recently, I set up a node.js server to run a python script using the python-shell . However, after migrating from Windows to Ubuntu, an EACCES error has been persistently popping up. Despite my attempts to adjust permissions and troubleshoot, I haven' ...

Order child property in AngularJSWe are sorting properties in

Greetings! I am in search of a way to arrange an angular list according to a specific child property. Here is the model that I currently have: $scope.data = [{name:"John",type:{talent:"genius",val:99}}, {name:"Paul",type:{talent:"genius", ...

Utilizing AJAX and jQuery to Cast Your Vote in a Poll

I have recently set up a basic poll using AJAX JSON jQuery to interact with the server side, but it seems to be malfunctioning. I would appreciate any guidance on where I may have made a mistake. I am still learning jQuery and JSON. JavaScript <script ...

Trouble arises with the chrome extension code for retrieving tweets from Twitter

I'm currently working on developing a Chrome extension that will display tweets featuring the hashtag perkytweets within the extension's popup. However, I'm facing an issue where nothing is being displayed. Let's take a look at the cod ...

How can I temporarily turn off the animation on a directive in Angular JS to prevent it from animating during page load?

Check out this JSFiddle for the issue: http://jsfiddle.net/goodwill/ezNuj/ I have created a custom directive with the code below: myApp.directive('ngFadeIn', function() { return function(scope, element, attr) { if (element.is('tr, t ...

Accessing variables from outside the query block in Node.js with SQLite

I'm looking to retrieve all the rows from a table and store them in an array called "arr". I need to access this stored array outside of the query section. Is there a way for me to get all the rows outside of the db.each function so that I can continu ...

How can I display a loading message in bootbox during the execution of an ajax process?

In my application, there is a page that displays status in a drop-down menu with two options - Active and Inactive. Users can change the status by simply clicking on the menu and selecting an option. To track this status change process, I am using JQuery/A ...

A straightforward redirection in Express involving a static file

Just starting out with Node and Express and encountering a bit of trouble. I have a static html page where users enter their username via ajax to my server, and then I want to redirect them to another html file. const express = require("express"); const b ...

Previously chosen selection for ui-select modified

Here is an example of HTML code using ui-select: <ui-select ng-model="actionSelectedId" on-select="changedAction($item, $select, row)" theme="bootstrap" ng-disabled="disabled"> <ui-select-match placeholder="Select an action"> ...

Vue Checkboxes - Maintain selection unless a different checkbox is selected

I have implemented a checkbox system with radio button behavior, but I am facing an issue where I want to keep the checkbox checked until another checkbox is selected (which will then uncheck the first one). I do not want the ability to deselect the checkb ...