Access methods through a parent component in a child with Angular 1

Asked

Viewed 865 times

0

Hello, I’m having a hard time getting information between 2 components working with Angular 1. This is my example:

angular.module("parent",["child"]).component("parent",{
  template : "<p> <child name></child> </div>",
  controllerAs:"parent",
  controller : {
    constructor(){}
  }
});

angular.module("child",[]).component("child",{
  binding : {
    "name" = "="
  }
  template : "<span>{{name}}</span>",
  controllerAs : "child",
  controller : {
    constructor(){
      this.name = name;
    }
  }
});

So far, in case I enter the value manually in <child name="foo"></child> the component Child has its contents changed. The problem is that I’m not understanding how through the Parent i will be able to change the content of the component name parameter Child

Could someone explain to me?

1 answer

1


What you can do is simply pass a property from the parent component to the attribute name. Therefore, by changing the value of this property in the parent component, the process of Binding of Angularjs will be in charge of propagating the change to the child component.

You are using the modifier of Binding =, That means the property name can be modified in both parent and child.

Example:

angular.module('parent', ['child']).component('parent', {
  template: 'parent: {{ parent.myName }} <br> child: <child name="parent.myName"></child>',
  controllerAs: 'parent',
  controller: function() {
    this.myName = 'foo';
  }
});

angular.module('child', []).component('child', {
  bindings: {
    name: '<'
  },
  template: '<span>{{ child.name }}</span>',
  controllerAs: 'child',
  controller: function() {
    this.name = 'bar';
  }
});

angular.element(function() {
  angular.bootstrap(document, ['parent']);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
<parent></parent>

  • in that case, if I pass the Binding as "<" i will be able to change only what was from Parent to Child? I arrived at a similar solution but, when changing Child, Parent also changed...

  • 1

    @Leandroluk Yes, that’s how you distinguish a component Dumb of a component smart, if you use the <, any change that needs to be made will have to be delegated to the parent component.

  • but then how can I have a Parent-only traffic lane for Child? I cannot change Parent because it is a direct Mutateobserver in mongodb, that is, it will change when it understands that there is the change made by Child in the bank

  • 1

    @Leandroluk Exactly using the <, where possession comes from the parent component. But if you want to update the parent data coming from the child, the ideal is to create the bind of a method for the child to send the change to the parent rather than changing the parent model.

  • my situation is, I have a list of users, which opens a div with a user’s Details. in my code today, when I change the Details, it is changing the list, and it should not be done because at the exact moment the bank changes, my code already updates "reactively"

  • 1

    @Leandroluk Is that probably the two are the same model. Using the < in Binding does not prevent this?

  • 1

    @Leandroluk I updated the answer with a working code. Also, there was a typo in the parameter bindings who was alone binding.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.