如何从隔离范围父作用域传递数据? [英] How to pass data from the isolate scope to the parent scope?

查看:152
本文介绍了如何从隔离范围父作用域传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的使用AngularJS,而我所要做的是创建一个指令,并在其中调用从父示波器功能。我能做到这一点,但我似乎无法弄清楚是如何从分离范围通过前pression父范围传递数据。这样做的角度开发者指南中的解释是有点混乱。

I am fairly new to using AngularJS, and what I am trying to do is create a directive and call functions from the parent scope within it. I am able to accomplish this, but what I can't seem to figure out is how to pass data from the isolate scope via an expression to the parent scope. The explanation of this in the Angular Developer guide is a little confusing.

该指令:

app.directive('myDir', function() {
    return {
        restrict: 'E',
        template: '<div ng-click="parentFunc(someValue)"><div>', 
        scope: {
           parentProp: '=property',
           parentFunc: '&func'
        },
        link: function(scope, element, attrs) {

        }
    }
});

的标记:

<my-dir prop="foo" func="bar(someValue)"></my-dir>

控制器:

app.controller('TstCtrl', function($scope) {
    $scope.foo = 'test';
    $scope.bar = function(value) {
       console.log(value);
    };
});

在此先感谢您的帮助!

Thanks in advance for the help!

推荐答案

有可能有其他的方法可以做到这一点,但看到这个小提琴,你会在一个对象使用传递到你的 FUNC 属性相同的参数名传递给函数。

There may be other ways to do this but see this fiddle where you would pass in an object to the function using the same parameter names passed into your func attribute.

    link: function(scope, element, attrs) {
        scope.parentFunc({someValue:'testing'});            
    }

因此​​,在这种情况下,既然你叫巴(someValue中) FUNC 属性你想传递在您的链接功能的 someValue中的对象。你打电话 parentFunc ,因为这是&放大器; FUNC。链接到您的分离范围

So in this case since you called bar(someValue) in your func attribute you would want to pass in an object with someValue in your link function. You're calling parentFunc because that's what &func is linked to in your isolate scope.

这篇关于如何从隔离范围父作用域传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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