Dynamic AngularJS data model

Custom Directive Code

(function() {

    'use strict';

    angular
        .module('app.colorslider.directive', [])
        .directive('colorSlider', [
            '$timeout',
            '$rootScope',
            'ColorSliderService',
            function($timeout, $rootScope, ColorSliderService) {
                return {
                    restrict: 'EA',
                    scope: {
                        array: '=',
                        shape: '=',
                        shapeindex: '=',
                        type: '='
                    },
                    templateUrl: 'views/directive/colorslider.html',
                    link: function(scope, elem, attrs) {

                        console.log(scope.type);

                        scope.fill = {
                            blue: 128,
                            green: 128,
                            red: 128,
                            opacity: 1
                        }

                        scope.stroke = {
                            blue: 128,
                            green: 128,
                            red: 128,
                            opacity: 1,
                            width: 1
                        }
                        scope.colorSlider = function() {
                            scope[scope.type].combined = ColorSliderService.rgbToHex(parseInt(scope[scope.type].red), parseInt(scope[scope.type].green), parseInt(scope[scope.type].blue));
                            console.log(scope[scope.type].combined);
                        };
                    }

                };
            }
        ]);
}());

HTML Implementation

<color-slider type="'stroke'" shape="selectedshape" array="linesArray" shapeindex="selectedshapeindex"></color-slider>

<color-slider type="'fill'" shape="selectedshape" array="linesArray" shapeindex="selectedshapeindex"></color-slider>

content of colorslider.html

<input class="colorInt" type="text" size="3" id="redInt" maxlength="3" value="{{[type].red}}" style="display: none">

I created the above directive to provide a means for selecting colors for both stroke and fill. The directive attributes allow the passing of the type into the directive's scope.

The directive is loading successfully but I have noticed that the colorslider.html template is not displaying any values. What could be the issue here?

Could it be incorrect to display a value in the directive template like this? value="{{[type].red}}"

Answer №1

This is an example of what the directive template will resemble:

<input class="colorInt" type="text" value="{{this[this.type].red}}" size="3" id="redInt" maxlength="3">

Within the template, this refers to the current scope object. Therefore, this.type in the template corresponds to scope.type in the link function. In the same manner, scope[scope.type].combined in the link function translates to this[this.type].combined in the template.

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

AngularJs - $watch feature is only functional when Chrome Developer Tools are active

My goal is to create a $watch function within a directive that monitors changes in the height and width of an element. This will allow the element to be centered on top of an image using the height and width values. Below is my app.js code: app.directive ...

The Iframe fails to load initially, but displays correctly upon manual refresh

Currently, I am working in an environment using Asp.net/C# and Visual Studio 2013 to develop a web application. I have created a page (aspx) with a single iframe on it, along with a JavaScript method that utilizes jQuery to inject HTML into that frame. & ...

Ways to avoid automatic error correction when running npm lint?

Utilizing the Vue CLI, I developed a Vue3 app with a Prettier/Eslint configuration. Upon making changes to the main.ts file as shown below: import { createApp } from "vue"; import App from "./App.vue"; import router from "./router& ...

Adding DOM elements dynamically from a controller in Angular.js is a powerful feature that

Hey there, I'm just getting started with Angular and may not be using the best practices yet. One thing I'm working on is appending a <circle> element within the controller function called placeShot(), which is triggered by ng-click='p ...

Combining audio files together with Node.JS

Edit: The initial question was deemed too broad, so I have simplified it to focus on one main query: How can I seamlessly play a sequence of audio files within a web page (specifically using React) without any gaps between each play? I have taken an aud ...

Analyzing Website Performance with Mozilla's FireBug Extension to Measure Page Loading Time

How amazing is the website stackoverflow? The responses are quick and helpful. I recently developed a simple JSP page that retrieves data from a database and displays it on the frontend. After using the Mozilla plugin FireBug Profiler, I noticed these va ...

Ways to display a specific element by its id while concealing others

I have a collection of items in a list, each with unique ids: <ul> <li id="item1">Item 1</li> <li id="item2">Item 2</li> <li id="item3">Item 3</li> </ul> <div class="item1">This is Item 1.</div> ...

Is there a way to display current data in angularJS based on the current time?

My database contains timestamps like 2016-12-30 00:30:10 which I retrieve using the $http module in Angular. Below is a snippet of my Angular code: angular .module('sampleApp') .controller('ReportCtrl', ReportCtrl) ...

Tips for developing a custom function to retrieve information from a WCF Rest service and showcase it in a table

In my code, I have created a function called service.js to get data from a WCF Rest API in JSON format. Then, I implemented another function called entryCtrl.js to fetch the data and display it in HTML. service.js (function (app) { app.service("CRUD_An ...

Invoking PHP function through Ajax

I'm having trouble with a PHP function not running when using AJAX. Although the AJAX post request is working correctly, here's the code snippet: function thumbs(i) { $('.thumbs-up' + String(i)).click(function(){ $(this).a ...

Separate the sender and receiver using Socket.io

My latest web application is built using the JavaScript stack (MongoDB, ExpressJS, AngularJS, NodeJS). I have successfully implemented registration, authentication, and a chat feature using Socket.io. However, I now need a method to distinguish between cli ...

Is there a way to display points on each vertex of my geometry using React, three.js, and Three-React-fiber?

I'm trying to figure out how to utilize the pointsmaterial and points object within three-react-fiber. Currently, I have a custom geometry that I've imported from a .gltf file and I'm rendering it like this: <mesh castShadow recei ...

"Encountered an error with resolving the dependency tree during the installation of npm react-facebook-login, known as ERESOLVE

Having trouble installing npm react-facebook-login in my react application due to dependency errors. It's a bit daunting, and I'm hesitant to forcefully install something that could lead to issues down the line. Since I am new to JavaScript, what ...

Checking for the existence of a Vue.js component

In the ready: method of the root instance, I have certain tasks to perform but only if some components are not present (not declared in the HTML). Is there a way to verify the existence of a component? ...

"Using the selected option from a dropdown list to pass to a PHP file for autocomplete functionality

Although there is no error in the code, I am facing an issue where, after selecting an option from the brands dropdown, when I type in the product field, it passes "%" instead of the brand id (1, 2, or 3). Is there a way to modify the code so that it passe ...

Using AngularJS API within a standalone function: Tips and tricks

I'm diving into the world of AngularJS and I want to make an HTTP GET request to a distant server without messing up my current view code. After some research, I discovered a way to execute a function right after the HTML is loaded by using a standalo ...

Encountering an issue while trying to read a text file using JavaScript

As I embark on my journey to understand how to read a text file locally using JavaScript and Ajax, I find myself lost in the sea of online tutorials. Despite diligently following these resources, I am unable to successfully display the contents of the .txt ...

Stealthy moments with angular-bootstrap-calendar

Check out this sleek and user-friendly calendar created with Angularjs. Visit the documentation here Is there a way to hide the event time on this calendar, as I don't need it for my purposes? ...

Carousel component failing to initialize in Bootstrap

When working with the carousel-inner dynamic elements in a class <div class = "active item"><img ..></div> <div class = "item"><img ..></div> <div class = "item"><img..</div> Make sure to execute the fol ...

Guide on developing a Vue modal for transferring user input to a separate component

I have been working on creating a modal component that allows users to input data and then displays it in another component. For example, the user is prompted to enter their first and last name in a modal component (Modal.vue). Once the user saves this inf ...