Search code examples
javascriptangularjsangularjs-scopeangular-ui-routerangularjs-components

access resolve in component controller in angularjs 1.5


I am trying to bind to an angularjs 1.5 component a resolve value without success, In the state definition, I have replaced the common value of template properties with a value of the name of my new component. like this:

.state('eventslogs.create', {
        url: '/create',
        template: '<addevent data="$resolve.addevent"></addevent>.',
        resolve: {
          addevent: newEventslog
        },
        data: {
          roles: ['admin'],
          pageTitle: 'Eventslogs Create'
        }
      })

NewEventslog is a function that injects one of my services

newEventslog.$inject = ['EventslogsService'];

  function newEventslog(EventslogsService) {
    return new EventslogsService();
  }

In my controller I have tried several ways but nothing works

angular.module('eventslogs')
  .component('addevent', {
    templateUrl: 'addevent.client.component.view.html',
    bindings: {
      data: '<'
    },
    controller: function($scope, $element) {
      var vm = this;
      vm.eventslog = vm.data;
    }

But vm.eventslog always results in an undefined value, what's wrong with my aproach?, If I use "@" instead "<" in bindings, vm.addevent results in a string with value "$resource.addevent" and not like an instance of newEventslog function.

I am using ui-route version 0.2.18


Solution

  • not sure but try this inside the component

    this.$onInit = () => {
      vm.eventslog = vm.data;
    }