angularjs 为特定类型的所有 html 元素赋予它们自己的作用域 [英] angularjs give all html elements of a specific type their own scope

查看:24
本文介绍了angularjs 为特定类型的所有 html 元素赋予它们自己的作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样做以利用 HTML5 中的

标签,我希望我网站上的每个 都有自己独特的使用 controllercontrollerAs 语法访问范围.

这是我认为可行的方法.

Javascript

\\Dialog 控制器函数对话框 () {返回 {范围: {},控制器:函数(){this.test = '对话测试';},controllerAs: '对话框',绑定到控制器:真}}angular.module('app',[]).directive('dialog', dialog);

HTML

<body ng-app='app'><dialog id='test'>{{Dialog.test}}</dialog>

我希望当对话框被激活时 Dialog.test 将评估为 Dialog Test.相反,它的计算结果是一个空字符串.更重要的是,如果我向主体添加控制器,则对话框可以访问其范围.就好像我的指令中的隔离范围定义被完全忽略了.

笨拙

请注意,由于大多数浏览器缺乏支持,我已将 plunk 修改为使用 而不是

.

http://plnkr.co/edit/eXtUq7BxCajOZAp8BpVe?p=preview

解决方案

您正在创建隔离范围,这是好事.但是在AngularJS1.2版本之后,他们做了一些突破性的改变,其中isolated scope将完全隔离.

因此 Span 指令的 范围 将仅对该指令的模板 (Span) 可见.

并且该指令的内部 html 将仅获得父/当前范围,而不是指令隔离范围(因为 Isolated Scope 将仅对模板可见).要打印 Span.test 的值,您必须创建模板并在指令中引用该模板,如下所示:

 var app = angular.module('test', []);函数 mainCtrl() {this.test = '测试';};函数 spanCtrl() {this.test = '跨度测试';}功能跨度(){返回 {范围: {},控制器:spanCtrl,控制器为:'跨度',模板:'{{Span.test}}'}}app.controller('mainCtrl', mainCtrl);app.directive('span', span);

<块引用>

您可以查看两个很棒的博客以了解更多详细信息 AngularJS 中的组件转入 AngularJS

I need to do this to make use of the <dialog> tag in HTML5, I want every <dialog> on my site to have its own unique scope accessible using the controller and controllerAs syntax.

Here is what I thought would work.

Javascript

\\Dialog Controller 

function dialog () {
    return {
        scope: {},
        controller: function () {
            this.test = 'Dialog Test';
        },
        controllerAs: 'Dialog',
        bindToController: true
    }
} 

angular.module('app',[]).directive('dialog', dialog);

HTML

<!-- Dialog HTML Example Case -->
<body ng-app='app'>
    <dialog id='test'>{{Dialog.test}}</dialog>
</body>

I would expect that when the dialog was activated Dialog.test would evaluate to Dialog Test. What happens instead is that it evaluates to an empty string. What's more is that if I add a controller to the body, the dialog has access to its scope. It is as though the isolate scope definition in my directive is completely ignored.

Plunk

Note that I have modified the plunk to use <span> instead of <dialog> due to the lack of support in most browsers.

http://plnkr.co/edit/eXtUq7BxCajOZAp8BpVe?p=preview

解决方案

You are creating isolated scope, thats good thing. But after AngularJS1.2 version, they have done some breaking changes, where isolated scope will be completely isolated.

So Span directive's scope will be visible to template of that directive(Span) only.

And inner html of that directive will get only parent/current scope only instead of directive isolated scope(As Isolated Scope will be visible to template only). To print value of Span.test, you have to create template and refer that template in your directive as below:

   var app = angular.module('test', []);
   function mainCtrl() {
       this.test = 'test';
   };
   function spanCtrl() {
       this.test = 'Span Test';
   }
   function span () {
       return {
           scope: {},
           controller: spanCtrl,
           controllerAs: 'Span',
           template: '{{Span.test}}'
       }
   }

    app.controller('mainCtrl', mainCtrl);
    app.directive('span', span);

You can checkout two awesome blog for more detail Component In AngularJS and Transclude In AngularJS

这篇关于angularjs 为特定类型的所有 html 元素赋予它们自己的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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