Unable to retrieve form values from a $modalInstance

When launching a $modalInstance, the user will need to select an option from dynamically loaded radio inputs and return the chosen value.

To open the $modalInstance, this function is used:

$scope.openAddFriendGroup = function () {
  var addFriendGroupModal = $modal.open({
    templateUrl: "addFriendGroupModal.html",
    controller: ModalAddFriendGroupCtrl,
    resolve: {
      myDetails: function () {
        return $scope.info;
      }
    }
  });
};

The controller for the $modalInstance is as follows:

var ModalAddFriendGroupCtrl = function ($scope, $modalInstance, myDetails, groupsSvc) {
  $scope.addableFriends = [];
  $scope.chosenFriend = null;

  // Function to retrieve the value of $scope.addableFriends when the $modalInstance loads

  $scope.addFriend = function () {
    $scope.recording = true;

    groupsSvc.addFriend($scope.chosenFriend, myDetails).then(function (response) {
      if(response.status && response.status == 200) {
        $modalInstance.close($scope.userid);
      }
    }, function (err) {
      $modalInstance.dismiss(err);
    });
  };
};

This is the HTML code for the modal itself in addFriendGroupModal.html:

<div id="add-friend-group-modal">
  <div class="modal-header">
    <h3 class="modal-title">Add friend</h3>
  </div>
  <div class="modal-body">
    <form class="form" role="form" ng-hide="loading">
      <div class="form-group">
        <label class="control-label">Friend:</label>
        <div class="radio" ng-repeat="thisFriend in addableFriends">
          <label>
            <input type="radio" id="friend{{thisFriend.id}}" name="chosenFriend" ng-model="$parent.chosenFriend" ng-value="thisFriend" /> {{thisFriend.name}} {{thisFriend.surname}}
          </label>
        </div>
      </div>
    </form>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary" ng-click="addFriend()">Add friend</button>
  </div>
</div>

The issue arises when attempting to retrieve the selected value from the radio buttons in the form within the modal. The value of $scope.chosenFriend remains null and does not update when an option is selected.

Can you spot what I might be doing wrong?

Answer №1

To utilize the promise returned by $modal.open, consider implementing the following code snippet:

 let notificationModal;

 $modal.open({ ... })
  .result.then(function(response){
      notificationModal = response;
   });

Answer №2

<input type="radio" id="friend{{thisFriend.id}}" name="chosenFriend" ng-model="$parent.chosenFriend" ng-value="thisFriend" /> {{thisFriend.name}} {{thisFriend.surname}}

Within this context, the ng-model is set to $parent.chosenFriend. Therefore, it would be more appropriate to anticipate $scope.chosenFriend to not be null. It is recommended to update the ng-model property to $scope.chosenFriend.

Answer №3

Recovered response from a similar inquiry, authored by gertas

Angular-UI utilizes transclusion in modals to connect modal content, causing new scope entries within the modal to be created in a child scope. This occurs with the form directive.

This issue is well-documented: https://github.com/angular-ui/bootstrap/issues/969

I have suggested a quick workaround that has proven effective for me using Angular 1.2.16:

<form name="$parent.userForm">

The userForm is generated and accessible in the controller's $scope. Thanks to scope inheritance, access to userForm remains intact in the markup.

<div ng-class="{'has-error': userForm.email.$invalid}"}>

Therefore, I would need to specify a name="$parent.friendForm" attribute on the <form> tag in my form, and then link the radio button model to it,

ng-model="friendForm.chosenFriend"
to retrieve it from the modal scope at $scope.friendForm.chosenFriend.

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

Utilizing mapped data to display numerous Material-UI Dialog elements

On my table, I have a list of users displayed. Each user has a button in their row to delete them. Clicking the delete button triggers a Material-UI Dialog to confirm. An issue arises where 3 dialogs are being rendered due to mapping, and the last dialog ...

Creating custom filters for data displayed in ui-grid using angularjs

This is the code I have developed: var app = angular.module('app', ['ui.grid']); app.controller('TableCrtl', ['$scope', '$filter', function ($scope, $filter) { var myDummyData = [{name: "M ...

Utilize AngularJS to present JSON data generated from PHP in a tabular format

After retrieving data from a MySQL database, I am formatting it into JSON. The fetch.php file: https://i.stack.imgur.com/4UbOs.png When I use echo $json;, the following is output to the console. [{"id":"1","emp_no":"1111","first_name":"1fname","last_n ...

Best practice for finding the parent element using Protractor

The recently released Guidelines advise against using the by.xpath() locators. I am making an effort to adhere to this suggestion, but I'm having difficulty locating a parent element. We are currently utilizing the .. XPath expression to access the p ...

A guide on utilizing buttons within ion list items to execute actions independently for both the button and the list item

Currently, I have a list item with a button that is displayed based on a certain condition. My issue is that when I click the button, a popup should appear, but instead, it also navigates to the next page in the background. Can someone help me figure out h ...

Troubleshooting problem with styling and animations in Angular Material input components

Take a look at the images provided below. The first image depicts the correct animations and styles of an input with a red underlined border. https://i.stack.imgur.com/lSzoL.png However, there are instances where this styling is not applied and the place ...

Quoting Properties in Javascript Objects

If we have the object below: var ObjectName = { propertyOne: 1, propertyTwo: 2 } Do we need to include quotes around the object properties like this? var ObjectName = { 'propertyOne': 1, 'propertyTwo': 2 } Is the sol ...

The jQuery script is malfunctioning

I have implemented an order form where users must complete a captcha code verification for cash on delivery. I am using jQuery to validate the entered captcha code. If the entered captcha code is incorrect, I prevent the user from submitting the form and ...

No information available at the moment

When the data is not present, it displays as "display none"... However, I want it to show "no data found" This is the current code if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].styl ...

Harness the power of the ioHook Node.js global native keyboard and mouse listener within your Browser environment

I'm dealing with a challenging issue that seems to have no solution due to security limitations. However, I'm reaching out to you as my last hope to find a workaround. For my project, I require a system that can monitor user mouse and keyboard a ...

Using jQuery to retrieve the TD value

I'm attempting to retrieve the TD value using the Value attribute.... Let's say I have the following HTML markup: <td nowrap="nowrap" value="FO2180TL" class="colPadding" id="salesOrderNumber1">bla bla </td> So, I tried this- v ...

Is it possible to create a button function in HTML that can alter the CSS image tag?

Is there a way I could create a function that can retrieve a value, update an image source, and then incrementally select another value? It would be incredibly helpful if you could provide a reference where I could learn how to do this or explain it in det ...

NodeJs is facing a challenge with the global variable for retrieving all routes methods after successful sign in

I have created a blog page with options for signing up and signing in. I used the req.locals.<name> method Using the GET method. Code snippet from server.js app.get('*',(req,res, next) => { res.locals.user = req.user || null; ...

Restricting the number of characters allowed for text messages and keeping track of the count

I am attempting to implement a character limiter for an html textarea using JavaScript. Additionally, I want to include a total character counter. Unfortunately, the code I have written isn't functioning as expected. Can anyone identify where my mist ...

The terminal in VS CODE is not able to detect Stripe

When I attempt to run the command 'stripe listen' in the VS Code terminal, an error is thrown: The term 'stripe' is not recognized as the name of a cmdlet, function, script file, or operable program. Please check the spelling of the ...

Troubleshooting the Confirm Form Resubmission problem on my website

Hello everyone! I'm working on a website and facing an issue where refreshing the page triggers a confirm form resubmission prompt. Could someone please advise me on how to resolve this? Thank you in advance! ...

Clearing form data after submitting in Laravel using axiosIn Laravel, utilizing

When working on my Laravel app, I encountered an issue while submitting a form using Vue and Axios. Despite my attempts to clear the input field after submission, it doesn't seem to work. HTML: <form method="post" enctype="multipart/form-data" v- ...

Strip newline characters from information in AngularJS

What is the recommended approach for detecting and formatting the "\n\n" line breaks within text received from the server before displaying it? Check out this Fiddle: http://jsfiddle.net/nicktest2222/2vYBn/ $scope.data = [{ terms: 'You ...

What is the process for crafting a Vuejs controlled input form?

Although I am familiar with creating controlled components in React using state, I am new to the Vue framework and would like to understand how to adapt my code from React to be compatible with Vue. My goal is to store the values of input fields in a data ...

Adding dynamic elements to a pop-up window with the use of jQuery's .load() function

When implementing Javascript to create a modal, I have encountered an issue where the close button disappears. The modal opens and loads different HTML files dynamically using the jQuery load() method, but the close button vanishes. I suspect that the mod ...