Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Angular 1.5 components as routes #2547

Closed
jonricaurte opened this issue Feb 10, 2016 · 17 comments
Closed

Use Angular 1.5 components as routes #2547

jonricaurte opened this issue Feb 10, 2016 · 17 comments

Comments

@jonricaurte
Copy link

Make ui-router have a similar feature as ngRoute does here using angular 1.5:

https://docs.angularjs.org/guide/component#components-as-route-templates

@jonricaurte
Copy link
Author

Looks like the change was added to ngView here:

angular/angular.js@983b059

@jonricaurte
Copy link
Author

Looks like it will need to be added here:

https://github.com/angular-ui/ui-router/blob/master/src/viewDirective.js

@christopherthielen christopherthielen added this to the 1.0.0-beta milestone Feb 11, 2016
@christopherthielen
Copy link
Contributor

thanks, yes this is definitely on our radar. Targeting 1.0-beta

@jonricaurte
Copy link
Author

Looks like 1.0alpha was merged to master after I left the previous comment. Looks like the change would be here for angular 1:

https://github.com/angular-ui/ui-router/blob/master/src/ng1/viewDirective.ts

Was wondering if there was a timeframe for the beta?

Thanks!

@jonricaurte
Copy link
Author

@christopherthielen Tried it out on the legacy branch with this commit (jonricaurte@a4cab7d) and seems to work. Will work on a PR with tests. Does not seem like anything else needs to be added.

@felixfbecker
Copy link

This is possible, but it would be nice to have $resolve like ngRouter

@BrianCerasuolo
Copy link

Would love to have this sooner rather than later

@smajl
Copy link

smajl commented Feb 12, 2016

Yes, please, $resolve and resolveAs much needed! So much boilerplate will go away for us who are adopting component routing.

@lucianogreiner
Copy link

+1

@mgamsjager
Copy link

+1 for $resolve

@felixfbecker
Copy link

Also, $resolve should not only be available in state templates, but also in UI Bootstrap modals :)

@christopherthielen
Copy link
Contributor

UI-Router 0.2.18 routing to angular 1.5 components: http://plnkr.co/edit/JDj7lFpKKsJBykrsZQGZ?p=info

The guts are here:

"use babel";

let module = angular.module('ui.router.components', ['ui.router']);

function bindResolves() {
  var names = controller.$inject = ['$scope'].concat(Array.prototype.slice.call(arguments));

  function controller($scope) {
    var values = Array.prototype.slice.call(arguments);
    for (var i = 1; i < values.length; i++) {
      $scope[names[i]] = values[i];
    }
  } 

  return controller;
}

let unnestR = (acc, array) => acc.concat(array);

function registerComponentDecorator($stateProvider) {
  $stateProvider.decorator('component', function(stateDef, parent) {

    if (stateDef.component) {
      let cmp = stateDef.component;
      console.log(`state ${stateDef.name} routes to a component: ${cmp}`)
      stateDef.controllerProvider = function($injector) {
        let directive = $injector.get(stateDef.component + "Directive")
        let keys = directive.map(d => Object.keys(d.bindToController)).reduce(unnestR, []);
        return bindResolves.apply(null, keys);
      }

      stateDef.templateProvider = function($injector) {
        let directive = $injector.get(stateDef.component + "Directive")
        let keys = directive.map(d => Object.keys(d.bindToController)).reduce(unnestR, []);
        let attrs = keys.map(key => `${key}="${key}"`).join(" ");
        return `<${cmp} ${attrs}></${cmp}>`
      }
    }


    return stateDef.component;
  })
}


module.config(registerComponentDecorator); 
module.constant('bindResolves', bindResolves);```

christopherthielen added a commit that referenced this issue Mar 5, 2016
feat(uiView): Expose the resolved data for a state as $scope.$resolve
feat(uiView): Route to components using templates
closes #2175
closes #2547
@christopherthielen christopherthielen modified the milestones: 1.0.0-beta, 1.0.0-alpha.1 Mar 29, 2016
@bitflower
Copy link

I'm using 1.0.0 alpha 4 but the binding to .component does not work. The milestone of this issue was set to 1.0.0 alpha 1 though. Is this implemented or not?

@christopherthielen
Copy link
Contributor

@bitflower yes 1.0.0-alpha.4 should route to component. Can I see your code?

@bitflower
Copy link

It works, it was my mistake. Sorry but thanks!

@trashhalo
Copy link

trashhalo commented Jun 11, 2016

Heads up to anyone else who finds this plunkr above ☝️ it has a bug. It needs to kebob the attribute keys "myResolvedBinding" vs "my-resolved-binding". or else it will fail to inject resolves with capital letters. Also it should have probably been an one way binding < instead of an =.

@christopherthielen hopefully you'll consider backporting the code here to legacy its super hard to convince people to let you install an 1.0 alpha version to get component routing. or atleast put that in a bower repo somewhere for me to include more formally.

@christopherthielen
Copy link
Contributor

@trashhalo @Jared314 @fetters5

Use this polyfill for legacy branch: https://github.com/softsimon/ui-router-route-to-components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants