16

I have created a component that needs to have a reference to the object for which the component was created. I didn't make to work and all my trials have failed. Below, I try to describe the intention.

The component definition would maybe look like this:

angular
    .module('myModule')
    .component('myComponent', {
        templateUrl: "template.html",
        controller: [
            MyController
        ],
        bindings: {
            myObject: '='
        }
    });

function MyController(myObject) {
    var vm = this;

    vm.myObject = myObject;
}

In a service I would like to create my object like this:

function createMyObject(args) {
        var myObject = {some: data};

        myObject.ref = "<my-component myObject='{{myObject}}'></my-component>";
        return myObject;
    }

Question

How can I pass data to angular component tag? Do I have to switch back to a component directive to make it work?

Any ideas are greatly appreciated. Thank you.

4
  • If your intention is to manipulate your DOM, then yes, you should be using a custom directive. Commented Feb 28, 2016 at 11:42
  • @LJ.Wizard I don't wish to manipulate DOM. Below I posted a solution. Commented Feb 28, 2016 at 13:29
  • what the heck is a MarkerController ? is it the same as MyController? Commented Aug 26, 2016 at 7:30
  • @Matian2040 yes, I've corrected it. Commented Aug 26, 2016 at 7:34

1 Answer 1

17

Solution 1

In your template:

<my-component key='$ctrl.myObject'></my-component>

In code:

angular
    .module('myModule')
    .component('myComponent', {
        templateUrl: "template.html",
        controller: [
            'objectService'
            MyController
        ],
        bindings: {
            key: '=' // or key: '<' it depends on what binding you need
        }
    });

function MyController(myObject, objectService) {
    var vm = this;

    vm.myObject.whatever(); // myObject is assigned to 'this' automatically
}

Solution 2 - via Component Bindings

Component:

angular
.module('myModule')
.component('myComponent', {
    templateUrl: "template.html",
    controller: [
        'objectService'
        MyController
    ],
    bindings: {
        key: '@'
    }
});
function MyController(myObject, objectService) {
    var vm = this;

    vm.myObject = objectService.find(vm.key);
}

Usage:

function createMyObject(args) {
    var myObject = {key: ..., some: data};

    myObject.ref = "<my-component key='" + myObject.key + "'></my-component>";
    return myObject;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Zatziky, So you're not attempting to pass the object anymore? Rather you're passing a string to the component? Is my understanding correct?
@MichaelR It's been some while but generally the example in the question works but with a slight modification. Instead of myObject='{{myObject}}' you would use controller myObject='$ctrl.myObject'. The workaround in the answer is just a hack. I have modified the answer accordingly.
Note that key attribute must be in kebab-case. Example: if you want your key to be myProperty then attribute must be my-property.
@EdKolosovskiy The example I posted was working at the time of the writing. Isn't it working anymore?
Just try this this.$onInit = function(){ console.log(this.myObject); } Your problem will solve.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.