4

I am unable to get ui.router to load a component on index.html, through a localhost or otherwise:

index.html

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script src="//unpkg.com/[email protected]/release/angular-ui-router.js"></script> <!-- used to route views -->
    <script src="js/components.js"></script>

    <body ng-app="myApp">
    <div> <!-- main content -->
         <ui-view></ui-view>   <!-- this directs ui-router to put the home view here-->
    </div>

components.js

    var app = angular.module('myApp', ['ui.router']); //makes a call to the Angular API to create our app, configured with the ui.router plug-in
    //configure our view router
    app.config(function($stateProvider) { 
      //create our different views as objects
      var mainState ={
        name: 'main', //name of the object
        url: '', //url to point to, or that causes this view to be triggered
        component: 'home', //component that works with the data, loads the template
        resolve: { //data to bind to our component
          data: function(Resource) {
            console.log(Resource.test()) //returns "Hello World"
            return Resource.test() 
          }
        }
      }
      //call the states
      $stateProvider.state(mainState); 
    })
    app.service('Resource', function ($http) {
      var service = {
       test: function() {
          return "Hello world"
        }
      }
      return service;
    })
    app.component('home', {
      bindings: { data: '<'}, //make the data we loaded into the view from the factory available to this component
      template: "<p>{{data}}</p>", //this is the html that we will plug our data into
      controller: function () {
        console.log(this) //does not call
      }
    })

"Hello world" loads, as does data when I make $http gets through the service. But it does not get passed to the component. Any thoughts?

2 Answers 2

11

component attribute is available from [email protected](see here and in CHANGELOG.MD - it was added in 1.0.0-aplpha) so it's not available 0.3.1.

Here is working jsFiddle (with 1.0.3-beta)

Sign up to request clarification or add additional context in comments.

5 Comments

Ah. That makes complete sense. Thank you.
So where can we download ui-router 1.x, alpha or beta? I see that 1.0.0-beta.3 is out. But how do we get it? Where is the prebuilt minified .js file?
They don't make it easy to download. I found I could get it here: npmcdn.com/[email protected]/release/…
I too could not easily find the download. By inspecting their tutorials page (which makes it seem like the "stable" version supports this since their tutorial does...) I found their link: unpkg.com/[email protected]/release/…
@MiTa, are you sure about that? I saw that there is a polyfill for 0.2.18 angular ui-router, unfortunately it does not work at my app, so I was wandering if I am missing something. github.com/softsimon/ui-router-route-to-components
0

By default the data will be applicable on view with $ctrl controller alias

template: "<p>{{$ctrl.data}}</p>",

Demo Plunkr

Comments

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.