scope - angularjs 嵌入范围访问

标签 scope angularjs transclusion

我已经设置了一个带有标题和应用/取消按钮的通用对话框指令。
该对话框有一个孤立的范围。

对话指令的内容是嵌入的,因此它的范围是对话范围的兄弟:

来自 Angular js 文档:

However isolated scope creates a new problem: if a transcluded DOM is a child of the widget isolated scope then it will not be able to bind to anything. For this reason the transcluded scope is a child of the original scope, before the widget created an isolated scope for its local variables. This makes the transcluded and widget isolated scope siblings.



不过,这对我来说是一个新问题。嵌入的 DOM 应该能够在应用时响应对话框。因此,我想在对话框上设置一个“应用”属性并让嵌入的 DOM 观看它。这是不可能的,因为他们是 sibling !

我哪里错了?

最佳答案

我遇到了类似的事情,有两种方法(我知道)可以直接访问嵌入的范围。

第一种是在编译函数中自己创建范围,然后将其与克隆链接函数一起传递给 transclude 链接函数:

function compileFn(tElement, tAttrs, transclude) {
    return linkFn;
    function linkFn(scope, element, attrs) {
        scope = scope.$new();
        scope.name = attrs.works1;
        transclude(scope, function(clone) {
            element.find('div').append(clone);
        });
    };
}

第二个是创建一个 Controller 并注入(inject)预先绑定(bind)到新范围的 $transclude 服务。您的克隆链接函数将接收新范围作为其第二个参数:
function Controller($element, $attrs, $transclude) {
    $transclude(function(clone, scope) {
        scope.name = $attrs.works2;
        $element.find('div').append(clone);
    });
}

在这两种情况下,您都必须提供一个克隆链接函数来自己进行嵌入,而不是使用 ngTransclude。

http://jsfiddle.net/dbinit/wQC7G/6/两者的例子。

关于scope - angularjs 嵌入范围访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415241/

相关文章:

PHP 会更改循环的范围并在包含的文件中进行切换

ruby - `class` 定义范围与 `def` 方法范围内的局部变量

php - 使用 AngularJS 和 PHP 向 MySQL 表插入数据后显示空值

css - 父级应该如何为 Angular 的子组件设置样式?我想要一个不会被弃用的好解决方案

c++ - Visual C++ 6 变量作用域错误

javascript - clearInterval() 不工作

javascript - 在 Angular 上,如何通过模板内的括号表示法访问对象属性?

javascript - 如何使用 angularfire 将输入与 firebase 中的用户相关联?

Angular 遮挡 : X is not a known element

symfony - 渲染前检查模板是否存在