Constantly defining the fill color in an SVG with AngularJS

In the front end, I am using a constant as a select box that contains an SVG element. The objective is to insert the SVG into the DOM and have it functional.

(function () {
    'use strict';
    angular.module('app.constants', [])
        .constant('ServiceSettings', 
            'markersType': [
                {text: 'original', value: 'original', src: '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 151.3 242" style="enable-background:new 0 0 151.3 242;" xml:space="preserve"><style type="text/css">.st1{fill:url(#SVGID_1_);}.st2{fill:url(#SVGID_2_);}</style><g><g><path class="st0 marker-fill" fill="input.markerView.color" ng-style="{\'fill\': input.markerView.color}" d="M75.7,0v29.3c25.6,0,46.3,20.7,46.3,46.3S101.3,122,75.7,122v120c0,0,75.7-124.5,75.7-166.3S117.5,0,75.7,0z"/></g><g><linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="37.8335" y1="160.2691" x2="37.8335" y2="116.9304" gradientTransform="matrix(1 0 0 -1 0 240.266)"><stop offset="0" style="stop-color:#5AC9E5"/><stop offset="1" style="stop-color:#4FBCD6"/></linearGradient><path class="st1" fill="url(#SVGID_1_)" d="M75.7,242V122c-25.6,0-46.3-20.7-46.3-46.3H0C0,117.5,75.7,242,75.7,242z"/><path class="st0 marker-fill" d="M75.7,29.3V0C33.9,0,0,33.9,0,75.7h29.3C29.3,50.1,50.1,29.3,75.7,29.3z"/></g><g><linearGradient id="SVGID_2_" gradientUnits="userSpaceOnUse" x1="75.667" y1="209.5996" x2="75.667" y2="119.0651" gradientTransform="matrix(1 0 0 -1 0 240.266)"><stop offset="0" style="stop-color:#347C8D"/><stop offset="1" style="stop-color:#4FBCD6"/></linearGradient><path class="st2" fill="url(#SVGID_1_)" d="M75.7,29.3c-25.6,0-46.3,20.7-46.3,46.3S50.1,122,75.7,122S122,101.3,122,75.7S101.3,29.3,75.7,29.3z M75.7,107.7c-17.7,0-32-14.3-32-32s14.3-32,32-32c17.7,0,32,14.3,32,32S93.3,107.7,75.7,107.7z"/><circle class="st3" fill="#ffffff" cx="75.7" cy="75.7" r="32"/></g></g></svg>'}
            ],

The only issue I am encountering is with the color.

<path class="st0 marker-fill" fill="input.markerView.color" ng-style="{\'fill\': input.markerView.color}" d="M75.7,0v29.3c25.6,0,46.3,20.7,46.3,46.3S101.3,122,75.7,122v120c0,0,75.7-124.5,75.7-166.3S117.5,0,75.7,0z"/>

I have attempted to set the fill in two different ways, but neither of them seem to work.

$scope.styleMarker = function() {
    $scope.input.markerView = {};
    $scope.input.markerView.color = ServiceSettings.markersHex[$scope.input.iconColor.value][$scope.input.iconShade.value];
    generalService.buildElement($scope.input.markerView.src, $scope.input.markerView.color);
    $scope.input.markerView.src = $sce.trustAsHtml($scope.input.iconType.src);
    console.log($scope.input.markerView);
};

The only way I can make the `fill` color work is by manually adding the entire SVG script to the view, but I prefer to keep it as a constant to avoid duplicating the SVG script.

Answer №1

Having both the fill attribute and ng-style can cause issues. It is recommended to only utilize the fill attribute in this situation.

<path class="st0 marker-fill" ng-attr-fill="{{input.markerView.color}}" ....

For SVG attributes, always use ng-attr when setting an expression to an attribute. There is a known issue with Angular where SVG attributes may not be processed properly unless ng-attr is used. This could be due to SVG being executed before Javascript has a chance to intervene.

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 transfer variables defined in PHP to JavaScript?

Can the variables previously defined in JavaScript be used in the line that starts with $sql =? var southWestLat = map.getBounds().getSouthWest().lat(); var southWestLng = map.getBounds().getSouthWest().lng(); var northEastLat = map.getBounds().getNort ...

Activating a hyperlink within a Div element by clicking on the Div itself

In my card element, I have a link that, when clicked, opens a modal pop-up containing all the necessary project information. I am currently attempting to trigger a click event on that link whenever any part of the div is clicked. This task is for a school ...

Clear the input field value when the onClick event occurs, and retain the value if not changed

When I initially set a default value in the input field, it is important that this value is removed when clicked inside the field. However, if the inserted value is left blank and the mouse cursor moves away from the field, the default value must be restor ...

Prepare fixtures for commands in Cypress before executing the hook

One of my main tasks is to load the fixtures file only once and ensure it is accessible across all project files. To achieve this, I created a fixtures.js file with the following content: let fixturesData; export const loadFixturesData = () => { cy ...

Enhance your Kendo UI File Manager by incorporating image uploading and folder renaming capabilities

Currently utilizing the kendo UI file manager to navigate through my files and images. According to telerik documentation, only the functions to add folders and remove files and folders are supported. However, I am in need of the ability to rename both f ...

utilization of dynamic templates within directives in the AngularJS framework

When it comes to deciding on a template based on the date, I came across an interesting example. However, in that specific example, the templates were so simple that strings could have been used. In my case, I prefer using PHP to generate the templates, so ...

The Angular boilerplate and Twitter Bootstrap 3 make for a powerful combination in

I've been attempting to integrate Twitter Bootstrap 3 into this project found at https://github.com/ngbp/ngbp, but I haven't been able to find any information on how to do so. Could it be that: It's simply not possible It's not recomm ...

Creating an array of data from JSON using JavaScript and displaying it in a chart using ApexChart

I am attempting to generate a chart displaying the value of Bitcoin in Euro. The data is fetched from JSON and converted into an array for the ApexChart series data (ApexData['xbtToEuro']) along with a list of dates. Despite my console indicatin ...

Focus on the image displayed through instafeed.js

Using the Instafeed.js plugin to create a dynamic Instagram feed. Everything is working smoothly, but I'm struggling to center the images being retrieved by Instafeed. Despite trying various methods, I can't seem to get it to display as desired. ...

Adjusting the zoom level in leaflet.js ImageOverlay will cause the marker

Using ImageOverlay to display an image as a map with Leaflet.js, but encountering issues with marker positions shifting when changing the zoom level. Followed instructions from this tutorial, and you can find a code pen example here. // Code for markers ...

I must update the menu to show only one sub-menu at a time, ensuring that multiple menus cannot be displayed simultaneously

When I click on a menu option, it displays correctly, but when I click on another option, both are displayed. The goal is to only display one at a time. https://i.sstatic.net/J6vLf.png $('.sub-menu ul').hide(); $(".sub-menu a").click( ...

Manipulating Json files using node.js through the html input element (Front end development with AngularJs and Html)

I have multiple JSON array files that I need to read and display in the form of a table. The only editable part of the table should be the header, which corresponds to the keys of the JSON objects. This way, the user can customize the headers and those cha ...

Does Parsley.js offer a way to configure so that the parsley-success field is not added to an input if it should be completely ignored?

Currently, I am excluding one input and adding the success class (which is fine with me) $('form').parsley({ excluded: '[data-parsley-sum-total="all"]' }); However, there are several other inputs without any validations that I do not wa ...

"Exploring the process of extracting the chosen value from the ng-autocomplete directive and passing it

I've implemented a directive for auto complete / auto suggest in AngularJS, sourced from . It successfully retrieves data from the server and filters it accordingly. However, I'm encountering an issue when it comes to selecting and using the item ...

Running an Express middleware function

While watching a tutorial on the Express framework, I came across some interesting code used by the instructor for handling requests. Here is how the route is set up: router.route('/').post(authController.restrictTo('admin', 'l ...

AngularJS hash links are designed to smoothly redirect users to a different webpage URL

I am trying to create a link to an element on the same page. However, when I click the link it redirects me to the same URL with #id attached even though I have html5Mode enabled. Looking for guidance on anchor hash linking in AngularJS Here is my direct ...

Execute index.js code from index.html using NodeJS and Express

Currently, I am diving into the world of NodeJS and Express using Replit.com for a small project. The main objective is to develop a basic input field that, upon submission, will post to different channels such as Discord and Twitter. The piece of code be ...

Implement a feature where users can add text inputs to specific columns in a table by

Recently, I've been diving into jquery table manipulation. I've written a code snippet that is supposed to add a text input field to a specific column when clicked. In my case, the column is the 9th. Here's the code I have so far: $('# ...

Can image data be extracted from a canvas without needing to physically render the canvas?

I am currently working on an Angular application that utilizes Chart.js to create dynamic charts in various views. Additionally, the application has the capability to export data to a PDF format using the PDFMake JavaScript library, which is a basic tool a ...

Press the button to activate the function

<table class="calc" cellpadding=2> <td><input type="button" class="calc" id="screen" value="0" ></td> <td><input type="button" class="calc" id="screen" value="0" ></td> <tr> </ ...