2016-09-23 2 views
4

я смог ui.router загрузить компонент на index.html, через локального хоста или иным образом:Угловое - UI.Router не загружается компонент

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> 

компоненты. 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» загружается, также как и данные, когда я делаю $ http через службу. Но он не передается компоненту. Есть предположения?

ответ

10

component атрибут доступен из [email protected] (см here и в CHANGELOG.MD - он был добавлен в 1.0.0-aplpha), поэтому он не доступен 0.3.1.

Здесь работает jsFiddle (с 1.0.3-бета)

+0

Ач. Это имеет смысл. Спасибо. –

+0

Итак, где мы можем скачать ui-router 1.x, alpha или beta? Я вижу, что [1.0.0-beta.3] (https://github.com/angular-ui/ui-router/releases/tag/1.0.0-beta.3) отсутствует. Но как мы его получим? Где предварительно скомпилированный файл '.js'? –

+0

Они не позволяют легко скачать. Я нашел, что могу получить его здесь: https://npmcdn.com/[email protected]/release/angular-ui-router.min.js –

0

По умолчанию данные будут применяться на просмотр с $ctrl контроллером псевдонима

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

Demo Plunkr

+0

Это тоже правильно. –

Смежные вопросы