AngularJS:在没有工厂,服务或广播的情况下将数据从控制器传递到控制器? [英] AngularJS : pass data from controller to controller without factory, service or broadcast?

查看:99
本文介绍了AngularJS:在没有工厂,服务或广播的情况下将数据从控制器传递到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用服务或工厂,并希望传递一个数据数组。我想从我的子组件访问我的父控制器中的数据。

I don't want to use a service or a factory and would like to pass for example an array of data. I would like to access data in my parent controller from my child component.

工厂和服务被排除在外,因为我最终想要将我的应用程序迁移到角度2,我不要我不想使用ngclick,它似乎与广播/ up / on不兼容。
如果有人知道如何使用广播在后台传递数据(没有用户交互,如输入或ngclick),它也会起作用:)

Factory and service are excluded since i eventually want to migrate my app to angular 2, and i don't want to use ngclick which seems inseperable with broadcast/up/on. If anyone knows how to pass data on the background (without user interaction, like input or ngclick) using broadcasting, it would work aswell :)

什么是我的选择?
谢谢!

What are my options ? Thank you !

推荐答案

如果您有嵌套组件,则可以使用 require

If you have nested components, you can access the parent's data with require

var child = {
    bindings: {},
    require: {
        parent: '^^parent'
    },
    controller: childController,
    templateUrl: 'child.template.html'
};

现在在您的子控制器中,您可以访问父控制器的实例,因此可以调用方法和访问它的属性:

Now in your child controller you have access to an instance of the parent controller and thus can call methods and access it's properties:

this.parent.parentMethod();

您在上一个答案中有一些更详细的代码:

You have some more detailed code in a previous answer here:

我应该在AngularJS应用程序的组件/控制器之间放置代码?

您的其他选择:

就像指令'范围 bindToController 您可以使用绑定组件的属性通过html属性绑定数据和方法

Just like directives' scope or bindToController you can bind data and methods through html attributes using the bindings propety of your component

<component-x shared="$ctrl.shared"></component-x>

var componentX = {
    bindings: { shared: '=' }
    ...



$ rootScope



决不要用它来存储数据。它可以工作,但它不是为了这个目的而制造的,并且会导致无法维护的代码。

$rootScope

Never use it to store data. It works but it's not made for that purpose and will lead to unmaintainable code.

这是一个常见的误解共享数据应该通过服务来完成。
这是1.5之前的真实和良好做法。

It's a common misconception that shared data should be done through services. It was true and good practice before 1.5 though.

另一个不好的做法( IMO)。
经典 MVC应用中,嵌套控制器可以使用 $ controller 服务继承父级:

Another bad practice (imo). In a classic MVC app nested controllers can inherit parents with the $controller service:

.controller('MainController', function ($scope) {

    $scope.logMessage = function(message) {
        console.log("Message: " + message);
    }

})

.controller('ServicesController', function($scope, $controller) {
    $controller('MainController', {$scope: $scope});
});



广播和发放事件



这是如果您正在广播的事件在应用程序范围内有意义(登录,注销等等),如果您要更新组件中的变量,请不要使用它。

Broadcast and emit Events

It's the way to go if the event you're broadcasting makes sense application wide (login, logout...etc.) If you're updating a variable in a component, don't use it.

这篇关于AngularJS:在没有工厂,服务或广播的情况下将数据从控制器传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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