"AngularJS Bi-Directional Data Binding Issue: Unexpected 'Undefined

Recently, I've been tackling a project that involves using a directive. Initially, everything was smooth sailing as I had my directive set up to bind to ngModel. However, I hit a roadblock when I needed to bind multiple values. Upon making the necessary changes to bind both value and staticValue, I found that the scope variable assigned to this became undefined. Check out the revised code below:

var app = angular.module('fieldSelectCheck', []);
app.directive('fieldSelectCheck', function () {

    var controller = ['$scope', function ($scope) {

        $scope.checkBoxClick = function () {
            if ($scope.isChecked === true) {
                $scope.fieldToggle = true;
            } else {
                $scope.fieldToggle = false;
                $scope.staticValue = "";
            }
        };

    }],

    url = 'Custom/BSI_iMIS_Importer/App/Directives/fieldSelectCheck.html';

    return {
        restrict: 'EA',
        templateUrl: url,
        scope: {
            fieldName: '@',
            fieldText: '@',
            options: '=',
            checkBoxClick: '@',
            isChecked: '@',
            fieldToggle: '=?bind',
            value: '=',
            staticValue: '='
        }, 
        controller: controller
    };
});

HTML Template

<div class="form-group row" data-ng-app="app">
    <label for="{{fieldName}}" class="col-lg-2 form-control-label text-right">{{fieldText}}</label>
    <div class="col-lg-3" data-ng-hide="fieldToggle">
        <select class="form-control" name="{{fieldName}}" data-ng-model="value" data-ng-options="option for option in options">
            <option value="">-- Please Select A Field --</option>
        </select>
    </div>
    <div class="col-lg-3" data-ng-show="fieldToggle">
        <input class="form-control" type="text" data-ng-model="staticValue" />
    </div>
    <div>
        <input type="checkbox" data-ng-model="isChecked" data-ng-change="checkBoxClick()" /> Static Value
    </div>
</div>

Implementation of Directive

<field-Select-Check 
    field-Name="{{fields}}" 
    field-Text="{{fields}}" 
    value="test" 
    static-Value="$parent[fields].staticValue"
    options="userFields"
    ng-repeat="fields in nameFields">
</field-Select-Check>

Answer №1

Make sure to keep the custom directive separate from the ng-repeat directive. By doing this, the custom directive can create isolate scopes while the ng-repeat directive can work with inherited scopes.

<div ng-repeat="fields in nameFields">
    <field-select-check 
        field-name="{{fields}}" 
        field-next="{{fields}}" 
        value="test" 
        static-value="valueList[fields]"
        options="userFields">
    </field-Select-Check>
</div>

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 fetch information from an API using Angular5 with Material2 components?

Here are the 'table.component.html' and 'table.component.ts' files where I am pulling data from an API to display in a table. Below is the object received from the API: [ {position: 1, name: 'Hydrogen', weight: 1.0079, sym ...

What is the best way to reveal the square's id value upon hovering over it exclusively?

In this assignment, I am refraining from using HTML and focusing on JavaScript to create functionality. Here's my progress so far: document.addEventListener("DOMContentLoaded", function () { let button = document.createElement('button' ...

There seems to be an issue with the accurate calculation of the screen width while utilizing the scrollbar-gutter

Please take note: The scrollbar-gutter: stable; property is not compatible with Safari. Additionally, the issue seems to be specific to Chrome and works fine in Firefox. I have observed some unusual behavior when attempting to position elements fixed to t ...

Setting the state to an array of objects in React: A beginner's guide

Currently, I am developing a flashcard application using React. To store the data entered by the user, I have initialized my state as an array of objects that can hold up to 10 terms and definitions at a time: state = { allTerms: [ { ...

The press of the Enter key does not trigger the button

I'm facing an issue with a form that has just one field for user input and a button that triggers some jQuery to hide the login form when clicked. However, pressing enter after entering text causes the page to refresh... I'm starting to think th ...

A guide on transforming a JSON object representing an HTML element into a live HTML element for display on a webpage using C#, JavaScript, or jQuery

I am looking to retrieve a JSON object from a database and convert it into HTML format. Here is an example of the JSON structure: {"input":{ "name":"fname", "type":"text", "placeholder":"Ente ...

Reset all divs to their default state except for the one that is currently active using jQuery

Here's the plan for my script: Once a user clicks on a "learn more" button, it will reveal the relevant information in the corresponding box. I'm aiming to implement a feature where if you click on another "learn more" button while one box is a ...

Using jQuery to iterate through rendered HTML with the ForEach function

I am utilizing JS/jQuery code to extract the cell value of an ASP DetailsView control (rendered HTML), validate it against a condition, and hide a specific div based on the result. Specifically, the code is examining whether the cell value is formatted lik ...

Encountering an issue... invariant.js:42 Error: A `string` value was received instead of a function for the `onClick` listener

Currently, I am working on creating a form that allows users to build quizzes without any restrictions on the number of questions they must include. To achieve this, I plan to add an "add question" button at the bottom of the quiz form, allowing users to d ...

Can you explain the term used to describe when specific sections of the source code are run during the build process to improve optimization?

One interesting feature I've noticed in next.js is its automatic code optimization during the build process. This means that instead of running the code every time the app is executed, it's evaluated and executed only once during the build. For ...

Having issues with transferring user input on change to a parent component in React JavaScript

I encountered an issue while passing a method from a parent component to a child component. The parent component, FilterableProductTable, contains a state called filterText. It renders a child component named SearchBar and passes down a function called han ...

Issue with hashtag in regular expressions

Looking for a way to identify and link hashtag words with an anchor tag? Here's a snippet showcasing the concept: <p class="display"></p> var content = "I enjoy #blueSky. My favorite color is #green. #sky is amazing"; ...

Employing the identical directive within a directive [angularjs]

My requirement is to utilize the same directive within another directive, based on a conditional parameter. However, every time I attempt to do this, it appears to enter an infinite loop. It seems like the templates are being preloaded and causing a recurs ...

What is the best way to extract data from an array of objects in a JSON response?

I am currently exploring the utilization of API's in React. Right now, I am developing a search input feature for country names using the Rest Countries API. The data is being pulled from . However, I am facing a challenge in handling this data as it ...

Property computation being initiated before property initialization

I am encountering an issue in my Vue application where I am trying to filter an array received from map getters based on a type prop within a computed property. Despite verifying that both the array elements and the prop are strings, the filtering process ...

What are some reasons for the slow performance of AWS SQS?

My current project involves measuring the time it takes to send a message and receive it from an SQS queue. Surprisingly, the average time it takes is between 800-1200 ms, which seems like an excessively long period. Below is the code I have been using for ...

Decode a JavaScript string without the need to save it as a file

Imagine a situation where I allow a client to input a JavaScript script that needs to be sent to my server. This script is meant to export an object. Here is the frontend code snippet: <form action="/blabla.js" method="post"> ...

Ways to transfer the value of a JavaScript variable to a PHP variable

Similar Question: How can I transfer JavaScript variables to PHP? I am struggling to assign a JavaScript variable to a PHP variable. $msg = "<script>document.write(message)</script>"; $f = new FacebookPost; $f->message = $msg; Unfort ...

In AngularJS, showcasing five elements at a time from an array containing 'n' items before looping back to the beginning

I'm a beginner in AngularJS and facing a scenario where I need to display 5 items from an array containing multiple items. The requirement is to initially show items 1-5 and then, after 2-3 seconds, add a 6th item at the top and remove the last item f ...

What steps are involved in creating a default toolbar for a material picker in a React application?

Looking for assistance with customizing the toolbar for material picker (v3) by adding a title inside the dialog. I successfully implemented this in the KeyboardDatePicker following a helpful thread (https://github.com/mui-org/material-ui-pickers/issues/11 ...