How can one create a form in AngularJS using ng-model and ng-repeat to dynamically populate data from a UI model description

Check out the JSFiddle link here: http://jsfiddle.net/vorburger/hyCTA/3/. It showcases an innovative "UI modeling" concept I came up with using AngularJS. The form is not directly coded in the HTML template; instead, it's controlled by uimodel JSON which defines how the datamodel should be displayed and edited:

<div ng-repeat="auimodel in uimodel">
    <label>{{$index + 1}}. {{auimodel.label}}</label>
    <input ng-model="datamodel[auimodel.model]" type="{{auimodel.type}}" />
</div>

The issue arises when the 'model' becomes more complex than a simple property and includes a 'path'. This breaks my square bracket dynamic keys approach, as shown in the faulty JSFiddle link: http://jsfiddle.net/vorburger/8CxRC/1/. Do you have any suggestions on how to handle this scenario?

Alternatively, would implementing a custom directive be the only solution for such cases? I prefer to avoid that route in order to maintain simplicity in creating and managing these UI model "meta templates."

Answer №1

I have recently discovered a method (perhaps not the most optimal) to accomplish this task on my own. Check out http://jsfiddle.net/vorburger/8CxRC/3/ for more details - it involves utilizing my square bracket dynamic keys approach, but with some preprocessing steps:

   for (var i = 0; i < $scope.uimodel.length; i++) {
        var resolvedModelProperty = $scope.datamodel;
        var remainingPath = $scope.uimodel[i].model;
        while (remainingPath.indexOf('.') > -1) {
            var nextPathSlice = remainingPath.substr(0, remainingPath.indexOf('.'));
            resolvedModelProperty = resolvedModelProperty[nextPathSlice];
            remainingPath = remainingPath.substr(remainingPath.indexOf('.') + 1);
        }
        $scope.uimodel[i].modelB = resolvedModelProperty;
        $scope.uimodel[i].modelL = remainingPath;
    }

Answer №2

Your project aligns closely with what I am working on, called SynthWidget. SynthWidget's equivalent to your needs can be found in the synthwidget.angular.widgetprocessor.AngularWidgetProcessor:

https://github.com/synthwidget/synthwidget/blob/master/modules/js/angularjs/src/main/webapp/lib/synthwidget/angular/synthwidget-angular.js

While I personally prefer using dot notation over square brackets (e.g., foo.bar.baz), there is one crucial line of code:

$compile( widget )( scope.$parent );

This line magically transforms the raw HTML you generate dynamically into fully operational Angular code.

If you have a chance to check out SynthWidget, I would love to hear your feedback! You can start the tutorial here.

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

Can JavaScript be used to mimic selecting a location from the autocomplete dropdown in Google Maps API 3?

Currently, I am attempting to automate the process of selecting items from the autocomplete dropdown in the Google Maps API v3 places library using jQuery. However, I am facing difficulty in identifying the necessary javascript code to select an item from ...

Does Firestore arrayunion offer any kind of callback function?

Hey there! I'm currently working on a voting system and I want to prevent the same user from voting multiple times on the same post. let db = firebase.firestore(); var postRef = db.collection("posts").doc(this.pid); postRef.update({ ...

Learn how to showcase real-time stock information from Yahoo Finance on an Android device. See the JSON format provided below for guidance

finance_charts_json_callback( { "meta" : { "uri" :"/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/", "ticker" : "ptc", "Company-Name" : "PTC Inc.", "Exchange-Name" : "NMS", "unit" : "MIN", "timezone" : "EDT", "curr ...

Utilizing Node.js modules in a frontend HTML file with the help of browserify

I am encountering difficulty using a node.js module on my website with browserify. The example provided only demonstrates how to run a separate JavaScript file, not how to incorporate the node.js module within the .html file itself. This seemingly simple i ...

What is the method for retrieving the status of an xmlHttpRequest?

I'm curious about how to verify the status of an xmlHttp request once it has been sent. Can someone explain the process to me? Thank you. function sendRequest(){ //retrieve access token var accessToken = 'xxx'; //get user_id ...

Challenges with Angular directive scopes

My current task involves taking an existing template and Angularizing it. I am dealing with 3 directives: a card, a card-header, and a card-body: <card> <card-header title="My Card"> <input type="text" ng-model="userSearch" /&g ...

A step-by-step guide on effectively swapping out every element in an array with its corresponding index location

After pondering over this question and conducting a search to see if it has been asked before, I couldn't quite find the answer due to difficulty in phrasing my inquiry. In case this question has already been addressed, I apologize for any duplication ...

Ensure that jquery.load is executed before the HTML content is loaded

Recently, I encountered a situation where I had a bootstrap navbar stored in a separate file, specifically named navbar.html. I utilized Jquery.load() to load this navbar into my HTML page. At the bottom of my HTML page, I included the following code: &l ...

If the token is null, my intention is to return to the sign-up page, but unfortunately, I am unable to redirect the page back

import React from 'react'; import { AppBar,Typography , Button , Toolbar} from '@material-ui/core'; import {Redirect, useHistory} from 'react-router-dom'; const Admin = () =>{ const history = useHistory(); let ...

Trouble with initializing the Ionic map controller

I have a mobile app with 5 tabs, one of which features a map. However, the map only loads when directly accessed through the URL bar. It seems that the controller is not loaded when navigating to the map tab through the app, as indicated by console logs. S ...

The .css() method does not apply any styles to the elements in the document

Having trouble with a jQuery function: .css() This is the HTML code in question: <div class="..." id="about"> // OTHER HTML CODE </div> And here's the corresponding jQuery code: (function($, window, document) { $(function() { ...

Exploring the basics of caching in Node.js and Express: a beginner's

Picture an application with numerous users and small sets of data (up to 10 name/value pairs), but the process to access this data involves complex calculations. The concept is based on a system with a 'history' state that is crucial for obtaini ...

Tips for efficiently utilizing both client-server side rendering and static generation rendering in web development

I am looking to implement static generation for top products using getStaticProps. Currently, there are sections in my rendering that do not require static generation, such as comments and related products. Here is the full code snippet: export default fu ...

Activate the 'li' element within the 'ul' whenever a 'ui-sref' link on the

I have been working on building a dashboard that consists of an abstract state called dashboard, with subdashboard as the immediate child state. https://i.sstatic.net/YTHky.png Previously, clicking on user details would take the user to the dashboard.tab ...

The expiration and creation of access tokens in the Gmail API

For the past 6 months, I have been utilizing the Google API to send emails from my server in a Node.js project. I have successfully set up credentials and generated both refresh and access tokens, which I have been using consistently. oAuth2Client = new go ...

reveal.js: Upon a fresh installation, an error has been encountered: ReferenceError - 'navigator'

Currently, I am attempting to integrate reveal.js into my React.js/Next.js website using the instructions provided at However, upon implementation, I encountered the following error: Server Error ReferenceError: navigator is not defined This error occurr ...

Vue removes existing data values post @selection change in the select box

I am facing an issue with my code where the data property in Vue.js appends the current result to the previous one instead of replacing it when a new option is selected from the select tag. I want the current data to be deleted and replaced with the newly ...

Error in React 15.1 when loading an image leads to null reference issue

Recently, I encountered a peculiar error right after updating from react 0.14 to react 15.1: ReactDOMComponentTree.js:105 Uncaught TypeError: Cannot read property '__reactInternalInstance$wzdyscjjtuxtcehaa6ep6tj4i' of null It seems that an im ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

How can I integrate a personalized button into an Angular Material autocomplete feature?

Currently, I am utilizing an autocomplete input component that is functioning well but lacks a specific feature. This component retrieves all the required data through a query when the page loads and automatically selects a default entry. Despite having ...