简体   繁体   English

组件路由器中的绑定-Angular 1.5

[英]Binding in Component Router - Angular 1.5

I'm struggling with binding in Component Router. 我在组件路由器中苦苦挣扎。

It is said in the Developer's Guide you should avoid using $scope in components therefore objects have to be passed through binding. 在《开发人员指南》中,您应该避免在组件中使用$ scope,因此必须通过绑定传递对象。

Based on examples in: https://docs.angularjs.org/guide/component and https://docs.angularjs.org/guide/component-router I came up with: 基于以下示例: https : //docs.angularjs.org/guide/componenthttps://docs.angularjs.org/guide/component-router我想到了:

HTML: HTML:

<div ng-app="app" ng-controller="MainCtrl as ctrl">
    {{ ctrl.hero.name }}
    <app></app>
</div>

Javascript: Javascript:

'use strict';

var app = angular.module('app', [
    'ngComponentRouter',
    'testComponent',
])

.config(function($locationProvider) {
    $locationProvider.html5Mode(true);
})

.value('$routerRootComponent', 'app')

.controller('MainCtrl', function(){
    this.hero = {
        name: 'Spawn'
    };
})

.component('app', {
    template: '<ng-outlet></ng-outlet>',
    $routeConfig: [
        {path: '/test', name: 'Test', component: 'testComponent'},
    ],
})

var testComponent = angular.module('testComponent', []);

testComponent.component('testComponent', {
    template: '<span>Name: {{$ctrl.hero.name}}</span>',
    controller: TestComponentController,
    bindings: {
        hero: '=',
    }
});

function TestComponentController() {
}

But <span>Name: {{$ctrl.hero.name}}</span> doesn't show "Spawn" or anything. 但是<span>Name: {{$ctrl.hero.name}}</span>不显示“ Spawn”或任何其他内容。

You cannot use the "bindings" definition in router components as the component doesn't have any HTML use that you would control. 您不能在路由器组件中使用“绑定”定义,因为该组件没有您要控制的任何HTML使用。

If you need to pass in data to the routing component you would access routing parameters in the $routerOnActivate callback. 如果需要将数据传递到路由组件,则可以在$ routerOnActivate回调中访问路由参数。

https://docs.angularjs.org/guide/component-router https://docs.angularjs.org/guide/component-router

Sample code to get started here: https://github.com/brandonroberts/angularjs-component-router/ 从此处开始的示例代码: https : //github.com/brandonroberts/angularjs-component-router/

I don't think there is a good solution for that yet in the ngComponentRouter for angular 1.x. 我不认为在AngComponent 1.x的ngComponentRouter中有什么好的解决方案。 Since it's still under active development, I am hopeful that a better solution will arise in the next iterations. 由于它仍在积极开发中,因此我希望在下一个迭代中会出现更好的解决方案。

In the meantime, what I do is that I make the component depends on its parent via require. 同时,我要做的是使组件通过require依赖于其父项。

EDIT: I understand now that you want to keep MainCtrl as the top controller, so I have edited the code: 编辑:我现在知道您想要保持MainCtrl作为顶部控制器,所以我编辑了代码:

.component('app', {
    template: '<ng-outlet></ng-outlet>',
    bindings: {
      hero: '<' // we have to bind here since you want to pass it from MainCtrl
    },
    $routeConfig: [
        {path: '/test', name: 'Test', component: 'testComponent'}
    ],
})

var testComponent = angular.module('testComponent', []);

testComponent.component('testComponent', {
    template: '<span>Name: {{$ctrl.hero.name}}</span>',
    controller: TestComponentController,
    require: {
        appCtrl: '^app',
    }
});
function TestComponentController() {
  var ctrl = this;
  ctrl.$onInit = function(){
    ctrl.hero = ctrl.appCtrl.hero
  }
}

And then html should then be: 然后html应该是:

<div ng-app="app" ng-controller="MainCtrl as ctrl">
  <app hero="ctrl.hero"></app>
</div>

See working codepen: http://codepen.io/bchazalet/pen/qZYzXM?editors=1111 查看有效的Codepen: http ://codepen.io/bchazalet/pen/qZYzXM?editors = 1111

It's not ideal, because it introduces a dependency (in your case from testComponent to app) but it works. 这不是理想的,因为它引入了一个依赖关系(在您的情况下是从testComponent到应用程序),但是它可以工作。

如果您仍然需要此代码,那么我的绑定与html attr一起使用,因此您的html应该enter <div ng-app="app" ng-controller="MainCtrl as ctrl"> {{ ctrl.hero.name }} <app hero="ctrl.hero.name"></app> </div>和我认为应该bindings: { hero: '<' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM