Angular-UI.Router未加载组件 [英] Angular - UI.Router not loading component

查看:80
本文介绍了Angular-UI.Router未加载组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过本地主机或其他方式使ui.router加载index.html上的组件:

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

index.html

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/angular-ui-router@0.3.1/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

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"加载,当我使$ http通过服务时,数据也加载.但是它不会传递给组件.有什么想法吗?

"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?

推荐答案

component属性可从ui-router@1.0.0获得(请参见 CHANGELOG.MD 中-它已添加到1.0.0-aplpha中),因此0.3.1不可用.

component attribute is available from ui-router@1.0.0(see here and in CHANGELOG.MD - it was added in 1.0.0-aplpha) so it's not available 0.3.1.

此处正在运行 jsFiddle (带有1.0.3-beta版)

Here is working jsFiddle (with 1.0.3-beta)

这篇关于Angular-UI.Router未加载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆