Angular 1 - Using bindings in controller - Component

Asked

Viewed 152 times

0

I am using Angularjs, in creating a Component I need to access the bindings in my controller.

In html I’m calling Component so:

<component type='tipo' urlid='meuid'></component>

I’m using the second syntax for bindigs:

bindings: {
    type: '@',
    urlid: '@'
}

And on the controller I’m using:

function meuController() {
    var vm = this;
    console.log(vm);
    console.log(vm.type);
    console.log(vm.urlid);
}

I’m having this comeback:

inserir a descrição da imagem aqui

As it is possible to see in the image the attributes then in the controller(vm) but when I access them I get the return undefined.

How do I access these attributes?

  • Inside your component’s html, try using $Ctrl.addName

  • Using $Ctrl.html nameAtributo I am able to access the data. The problem is that I need to use them in my Component controller.

  • In my code I make a reference to the scope like this: "var self = this;", and in the controller, to access the attributes, I use self.attributeName

  • Ata, I messed up here, sorry, already tried to access using vm["type"] ?

  • Using vm['type'] I have the same return undefined.

2 answers

1

Follows code below:

vm.$onInit = function () {
    console.log(vm.type);
    console.log(vm.urlId);
}

So it will only access the bind when the controller is started.

0

I was able to access the data using $timeOut, thus:

$timeout(function () {
    console.log(vm.type);
    console.log(vm.urlId);
})

Perhaps there is some delay for Angular to recognize the parameters. This way I’m having the return.

Browser other questions tagged

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