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

查看:33
本文介绍了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.

工厂和服务被排除在外,因为我最终想将我的应用程序迁移到 angular 2,而且我不想使用 ngclick,这似乎与广播/向上/打开密不可分.如果有人知道如何使用广播在后台传递数据(没有用户交互,如输入或 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 :)

我有哪些选择?谢谢!

推荐答案

如果你有嵌套组件,你可以通过 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 应用程序的组件/控制器之间使用?

您的其他选择:

就像指令的 scopebindToController 一样,您可以使用组件的 bindings 属性通过 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天全站免登陆