Is there a way to assign multiple ng-model values to individual input fields generated by a single directive?

After developing a custom directive to dynamically include various form fields, I encountered an issue where the ng-model gets duplicated when more than one directive-based field is added. As someone new to working with directives, any assistance would be greatly appreciated.


var app = angular.module('app', []);
angular.module('app').controller('MainCtrl', function ($scope, $compile) {

$scope.add = function(ev,attrs){
  var iact = angular.element(document.createElement('itemactivo'));
  var el = $compile( iact )( $scope );

  //where do you want to place the new element?
  angular.element(document.body).append(iact);

  $scope.insertHere = el;
};

});


// directive
angular.module('app')
.directive('itemactivo', function () {
   return {
      templateUrl: 'itemactivo.html',
      restrict: 'E',
      link: function postLink(scope, element, attrs) {

       }
        };
});

A plunker showcasing the issue can be accessed here:

http://plnkr.co/edit/IDD5KFO7UEOnP8cSuN33?p=preview

Answer №1

To enable scope in your custom directive called itemactivo, you need to include the attribute scope: true. Your code should resemble the following:

angular.module('app')
  .directive('itemactivo', function () {
    return {
      scope:true,
      templateUrl: 'itemactivo.html',
      restrict: 'E',
      link: function postLink(scope, element, attrs) {
       // element.text('this is a chart');
      }
    };
  });

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

How can you transform this jQuery snippet into a unique Javascript function?

I've managed to get this code to work, but I'm looking for a way to streamline it using a JavaScript function. This would save me the hassle of writing it out repeatedly. $("#portfolio").waypoint(function() { for (var i = 0; i <= 8; i++) ...

Refreshing a page when using html5Mode with a base tag may not function properly

AngularJS - html5Mode Issue: Cannot GET /login After coming across a similar post regarding this issue, I implemented the base tag to ensure relative URLs work properly. Additionally, I have included $locationProvider.html5Mode(true); in my application. W ...

Showing the values of selected checkboxes in a select dropdown - here's how!

Link to the jsfiddle : https://jsfiddle.net/a1gsgh11/9/ The JavaScript code seems to not be working on the js fiddle platform. My main concern is that I would like the selected checkbox values to display immediately without needing to submit any buttons. ...

Having trouble displaying the Primevue dialog modal in Vue 3?

I am using [email protected] and [email protected] Main script import { createApp } from 'vue' import App from './App.vue' import router from './router' import PrimeVue from 'primevue/config'; import &apos ...

Refreshing SQL Server data using an HTML form

In the table below: <table id="skuTable" role="grid"> <thead> <th class="skuRow">Order</th> <th>Fab. Date</th> <th class="skuRow">Norder</th> <th>Color</th> ...

Order the rows being inserted into a table by iterating through them using a foreach loop directly from the

I am currently working on a table that receives its data from a database. The table structure includes the typical header row, and then I use a foreach() loop to fetch information from an included file and display it in the table body (with a new row for e ...

What is the functionality of react.js state?

As I delve into learning React.js, I've been quite impressed with its state management capabilities. However, I'm curious about the technical workings behind it. Does React.js utilize cookies or browser storage for its state management? ...

Exploring the archive of content with AJAX back button tracking

Greetings, Before considering this question as a duplicate, I would like to mention that I have thoroughly searched for similar questions but none of them address my specific issue. Below is a simple example, and any assistance you can provide would be hig ...

Necessary workaround needed for HTML time limit

Our HTML page has been designed to automatically timeout after 10 minutes of inactivity. This functionality is achieved through the following code: function CheckExpire() { $.ajax({ type:'GET', url:self.location.pathname+"?ch ...

Prevent automatic jumping to input fields

Can someone help me with a problem related to the html input tag or primefaces p:input? I am facing an issue where the cursor always automatically jumps into the input field. The height of my page is such that scrolling is required, and the input field i ...

Ensuring Form Validity with jQuery for Exclusive Radio Buttons

How can I display an error message when a radio button value is not equal to a specific value? I have a series of yes/no questions and want to show disclaimers under each set of buttons if the value provided is null or different from what is required. Most ...

Creating unique random shapes within a larger shape on a canvas, as shown in the image

I have a parent rectangle and would like to add up to 10 or fewer rectangles on the right-hand side corner of the parent rectangle, as shown in the image below: https://i.sstatic.net/RW9ix.png I attempted to write code to achieve this, but the alignment ...

Configuring properties for a component by retrieving data from MongoDB using an API request

My current API call utilizes axios in the following format: Service.get('path/to/api', (status, data) => { this.setState({ ComponentData: data, loaded: true}); }); {this.state.loaded && <Component id={this.state.ComponentD ...

Using jQuery to submit an array within a form - a comprehensive guide

My goal is to accumulate form data each time the user clicks add_accommodation and add it to an array that will be sent to the endpoint http://server/end/point. <form action="http://localhost:3000/a/b/c" method="post"> <div> <input ...

Bordering the limits with Highcharts

Can I enhance the plot area with a border, similar to the black border shown in the image? https://i.sstatic.net/KirsN.png -- UPDATE -- I am encountering some unwanted white space between the plot area and the border. Is it possible to eliminate this wh ...

Incorporate Material Design Lite and AMP into your Angular 5 project for a sleek

Looking to develop an e-commerce progressive web app using angular 5. Wondering how to incorporate AMP with angular 5 in Google Material Design Lite. If not viable, what are some alternative options worth considering? ...

Express Module Paths Failing to Function Properly

When I first started building my routes, I had everything in one api.js file. However, I realized there might be a better approach, so I did some research online to see how others handle it. After following a few tutorials, I decided on a new layout with s ...

Restrict the number of rows in a real-time search JSON data by utilizing the $.each method

Is there a way to limit the number of rows returned in live search JSON data through URL? I attempted to count the table rows and return false, but it did not work. Any suggestions on how to achieve this? $(document).ready(function() { $.ajaxSetup ...

if statement for a method within the ng-click directive

Whenever I click the button, I want to either execute createRole() if RoleName is not set or EditRole() method if RoleName is set. However, for some reason, EditRole() is being executed for both cases. <button type="submit" ng-click="selectedItem.Rol ...

Despite returning an "OK" status, the jQuery Ajax Codebehind Post fails to function properly

Attempting to call a function in ASP.NET with jQuery Ajax: var params = "{'name':" + "\"" + name + "\"}"; $.ajax({ type: "POST", url: "CreateTopic.aspx/CreateNewTopic", data: params, ...