Ways to transfer information from the Component to the parent in AngularJS 1.x

I am facing an issue with my component that consists of two tabs containing input fields. I need to retrieve the values from these input fields when the SAVE button is clicked and save them to the server. The problem lies in the fact that the SAVE function is located in the parent index.js, while the input fields are within the TestComponent.js. I have been struggling to find a way to extract the input values from the component and send them to the Parent Controller (indexController.js).

I attempted to use binding as a solution by storing all the data in an object and passing the object to the indexController.js, but unfortunately, it did not work as expected.

Please refer to the PLUNKER for a demonstration of the issue.

If anyone could provide assistance on this matter, it would be greatly appreciated.

index.html

<body ng-app="heroApp">

<div ng-controller="MainCtrl as vm">

  <!-- Component Started -->
  <md-tabs>
    <tab-component> </tab-component>
  </md-tabs>
  <!-- Component Ended -->

  <button type="submit" ng-click="save()"> Save </button>

</div>

</body>

index.js

(function(angular) {
  'use strict';
angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() {
  var vm = this;

  this.onStatusChange = function(data) {
    vm.mandatoryFilesIncluded = data;
  };

  this.save = function() {
    vm.requestInProgress = true;
    vm.Upload.upload({
      url: someURL,
      arrayKey: '',
      data: {
        file: vm.files,
        name: vm.data.name,
        title: vm.data.title,
        description: vm.data.description,
      }
    }).then(function(response){
        alert("data is uploaded.");
    });
  };

});
})(window.angular);

tabComponent.html

<!-- Upload Tab-->
<md-tab id="picTab" label="Pic">
  <div layout-gt-xs="row" layout-align="start center">

    <md-input-container style="padding-left: 0">
      <md-button>
        <md-icon class="material-icons">attach_file</md-icon>
      </md-button>
    </md-input-container>

  </div>
</md-tab>


<!-- Info Tab-->
<md-tab id="infoTab" label="Info">
  <md-content class="md-margin">

    <div layout-gt-sm="row">
      <!-- Name -->
      <md-input-container>
        <label>Name</label>
        <input ng-model="vm.data.name" name="name" required>
      </md-input-container>

      <!-- Title -->
      <md-input-container class="md-block" flex-gt-sm>
        <label>Title</label>
        <input ng-model="vm.data.title" name="title" required>
      </md-input-container>
    </div>

    <!-- Description field -->
    <div layout="row">
      <md-input-container class="md-block" flex-gt-sm>
        <label>Description</label>
        <textarea ng-model="vm.data.description" name="descriptionField" rows="1"></textarea>
      </md-input-container>
    </div>
  </md-content>
</md-tab>

tabComponent.js

(function(angular) {
  'use strict';
angular.module('heroApp').component('tabComponent', {
  templateUrl: 'tabComponent.html',
  controller: myComponentController,
  controllerAs: 'vm',
  bindings: {
    statusChange: '&',
  }
});
})(window.angular);

function myComponentController() {
  var ctrl = this;
  ctrl.mandatoryFilesIncluded = false;
}

Answer №1

When the save button is activated in the main controller, you have the option to specify vm.data within the controller and connect it to the component:

bindings: {
  data: '=',
}

The tab-component then transfers the data like so:

 <tab-component data="vm.data"> </tab-component>

Any modification made to vm.data within the component will also be reflected in the controller.

Check out the demo

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

Have you ever encountered issues with Promises.all not functioning properly within your vuex store?

I'm currently experiencing an unusual problem. In my Vue project, I have a Vuex store that is divided into different modules. I am trying to utilize Promise.all() to simultaneously execute two independent async Vuex actions in order to benefit from th ...

Saving JSON data into an HTML element using Handlebars templating

Is there a way to save the entire JSON object within an HTML element as a data attribute? let a = {name : "sample", age : "34"} $.find('#someDiv').data('adata', a); Is it possible to achieve the same result using Handlebars when creat ...

The system encountered an error while trying to access the property 'enabled' of an undefined object

When working on a reactive form in my code, I need to ensure the values are properly set for the controls. ngDoCheck() { setControlValues(); } ngChanges(changes: SimpleChanges): void { setControlValues(); } private setControlValues() { try { ...

Is It Possible to Create Flash Content Without Using a SWF File?

Is there a way to embed Flash directly in HTML, rather than linking to an external SWF file? I am looking to send an HTML form via email for the recipient to fill out by opening it in a browser. The final step would involve copying the result to their clip ...

AngularJS special filter is not retrieving any data

Having an issue with a new filter that is not receiving data: Here is my error log: Controller Code: angular.module('reklaApp') .controller('MainCtrl', function ($scope, $http, Reklas, Modal) { $scope.newRekla = {}; Reklas.getAll() ...

Assistance needed for iterating through JSON data

I need assistance retrieving specific data from a JSON response in my JavaScript code. Unfortunately, the current approach is not providing the desired results: This is my JavaScript code snippet: function retrieveArtists(response){ var artistList ...

methods for extracting header information using JavaScript

Is there a way to retrieve values from the header URL using JavaScript? Consider this scenario: www.example.com/content.html?param=value How can one extract this information upon redirecting to the page content.html? What techniques could be employed f ...

Creating a secure ZIP file with password protection using node.js

Currently, I am faced with the challenge of creating a ZIP file in node.js with password protection. Despite using the "node-zip" module for this task, it lacks the functionality to provide password protection: var zip = new require('node-zip') ...

When scrolling, apply a CSS class to a div element once it becomes visible on the

I'm in the process of developing a timeline feature for my website, and I am facing an issue where the addClass function is being applied to all sections, even those that are not currently visible on the screen during scrolling. If you would like to ...

Combining ng-filter and ng-repeat to iterate over key-value pairs in objects

Currently, I am utilizing the built-in Angular 1 filters in an attempt to filter out a property of an object. While Angular Filters typically only accept Arrays and not Objects, the structure of the web application prevents me from refactoring to pass an a ...

Observing a sessionStorage variable within AngularJS version 1.0.7

Is there a way to monitor a sessionStorage variable in AngularJS 1.0.7 without relying on a directive? For example: $scope.$watch("sessionStorage.getItem('OfferStore_items')", function() { console.log("New offer"); ...

The sole focus is on the animation within the div

Is it possible to have a circle animation within a button without overlapping it? Check out my code on this fiddle. $(document).ready(function(){ $('button').on("mouseup",function(){ $('#mousemark').removeClass("c ...

Eliminate repeated elements within a JSON dataset to create a consolidated array

Looking to extract unique data from the JSON object below in order to create a result json with a list of questions and their corresponding choices. Any assistance would be greatly appreciated. Thanks in advance..!! var data = [ { "category": "s ...

Trying out an ajax request in React by clicking a button

I have been working on testing a simple Login component that involves filling out an email and password, then clicking a button to log in. When the login button is clicked, it triggers an ajax post request using axios. I am interested in testing both happy ...

I'm confused as to why the icon color isn't changing on the Blogger homepage for each individual user

I'm experimenting with changing the eye color based on visitor count. It works perfectly fine on static posts in my Blogger, but it fails to update the eye color properly on my homepage according to the set numeric values. Instead of displaying differ ...

The Angular 1.5 directive utilizes one-way binding to seamlessly update the parent scope

I am experiencing an issue with a directive that has an isolated-scope and one-way binding variable. Despite setting up the directive to keep the scope separate, any changes made to the variable in the directive controller also update the parent scope. Be ...

`The best way to access a JavaScript variable from PHP`

This is the code snippet I am working on: var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; document.form1.bookingsession.focus(); var coords = { "lat": daaa }; $.get('bs.php', coords, function () { ...

Steer clear of including optional dependencies when using Yarn for package installations

Within my yarn.lock file, there is a single reference to momentjs: pikaday@^1.6.0: version "1.6.1" resolved "https://registry.yarnpkg.com/pikaday/-/pikaday-1.6.1.tgz#b91bcb9b8539cedd8dd6d08e4e7465e12095671b0" optionalDependencies: moment "2.x" ...

Utilize a MongoDB query to locate a single document and include a conditional statement within the update operation

I've been struggling with this problem for quite some time now and I could really use some guidance from experts here on SO. Essentially, what I am trying to achieve is to locate a specific document using a provided _id, and then execute a conditional ...

How about representing a two-dimensional array in a point-free manner?

Exploring functional/tacit style programming, specifically implementing the snake game (example: ) The main issue at hand involves processing an array of strings like: [ ['2 '], ['10'] ] and obtaining a list of coordinates in numer ...