当前位置: 首页 > news >正文

一个wordpress多个网站网络优化主要做什么

一个wordpress多个网站,网络优化主要做什么,泉州公司网页制作,杭州室内设计设计公司前十排名AngularJS Directive 隔离 Scope 数据交互什么是隔离 Scope AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性。通常使用这种直接共享的方式可以实现一些简单的 directive 功能。当你需要创建一个可重复使用的…
AngularJS Directive 隔离 Scope 数据交互

什么是隔离 Scope

AngularJS 的 directive 默认能共享父 scope 中定义的属性,例如在模版中直接使用父 scope 中的对象和属性。通常使用这种直接共享的方式可以实现一些简单的 directive 功能。当你需要创建一个可重复使用的 directive,只是偶尔需要访问或者修改父 scope 的数据,就需要使用隔离 scope。当使用隔离 scope 的时候,directive 会创建一个没有依赖父 scope 的 scope,并提供一些访问父 scope 的方式。

为什么使用隔离 Scope

当你想要写一个可重复使用的 directive,不能再依赖父 scope,这时候就需要使用隔离 scope 代替。共享 scope 可以直接共享父 scope,而隔离 scope 无法共享父scope。下图解释共享 scope 和隔离 scope 的区别:
图片

示例可看:

共享 scope

使用共享 scope 的时候,可以直接从父 scope 中共享属性。因此下面示例可以将那么属性的值输出出来。使用的是父 scope 中定义的值。

js代码:

app.controller("myController", function ($scope) {
    $scope.name = "hello world";
    }).directive("shareDirective", function () {
    return {
            template: 'Say:{{name}}'
    }
});

html代码

<div ng-controller="myController">
<div share-directive=""></div>
</div>

输出结果:

Say:hello world

隔离 scope

使用隔离 scope 的时候,无法从父 scope 中共享属性。因此下面示例无法输出父 scope 中定义的 name 属性值。

js代码:

app.controller("myController", function ($scope) {
    $scope.name = "hello world";
}).directive("isolatedDirective", function () {
        return {
            scope: {},
            template: 'Say:{{name}}'
        }
});

html代码:

<div ng-controller="myController">
<div isolated-directive=""></div>
</div>

输出结果:

Say:

示例请点击:http://kin-sample.coding.io/angular/directive/share-and-isolated-scope.html

从上图可以看出共享 scope 允许从父 scope 渗入到 directive 中,而隔离 scope 不能,在隔离 scope 下,给 directive 创造了一堵墙,使得父 scope 无法渗入到 directive 中。

具体文档可以参考:https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive

如何在 directive 中创建隔离 scope

在 Directive 中创建隔离 scope 很简单,只需要定义一个 scope 属性即可,这样,这个 directive 的 scope 将会创建一个新的 scope,如果多个 directive 定义在同一个元素上,只会创建一个新的 scope。

angular.module('app').controller("myController", function ($scope) {
    $scope.user = {
            id:1,
            name:"hello world"
    };
}).directive('isolatedScope', function () {
    return {
        scope: {},
        template: 'Name: {{user.name}} Street: {{user.addr}}'
    };
});

现在 scope 是隔离的,user 对象将无法从父 scope 中访问,因此,在 directive 渲染的时候 user 对象的占位将不会输出内容。

隔离 scope 和父 scope 如何交互

directive 在使用隔离 scope 的时候,提供了三种方法同隔离之外的地方交互。这三种分别是

  • @ 绑定一个局部 scope 属性到当前 dom 节点的属性值。结果总是一个字符串,因为 dom 属性是字符串。
  • & 提供一种方式执行一个表达式在父 scope 的上下文中。如果没有指定 attr 名称,则属性名称为相同的本地名称。
  • = 通过 directive 的 attr 属性的值在局部 scope 的属性和父 scope 属性名之间建立双向绑定。

@ 局部 scope 属性

@ 方式局部属性用来访问 directive 外部环境定义的字符串值,主要是通过 directive 所在的标签属性绑定外部字符串值。这种绑定是单向的,即父 scope 的绑定变化,directive 中的 scope 的属性会同步变化,而隔离 scope 中的绑定变化,父 scope 是不知道的。

如下示例:directive 声明未隔离 scope 类型,并且使用@绑定 name 属性,在 directive 中使用 name 属性绑定父 scope 中的属性。当改变父 scope 中属性的值的时候,directive 会同步更新值,当改变 directive 的 scope 的属性值时,父 scope 无法同步更新值。

js 代码:

 app.controller("myController", function ($scope) {
        $scope.name = "hello world";
    }).directive("isolatedDirective", function () {
        return {
            scope: {
                name: "@"
            },
            template: 'Say:{{name}} <br>改变隔离scope的name:<input type="buttom" value="" ng-model="name" class="ng-pristine ng-valid">'
        }
})

html 代码:

<div ng-controller="myController"><div class="result">
       <div>父scope:
           <div>Say:{{name}}<br>改变父scope的name:<input type="text" value="" ng-model="name"/></div>
       </div>
       <div>隔离scope:
           <div isolated-directive name="{{name}}"></div>
       </div>
        <div>隔离scope(不使用{{name}}):
             <div isolated-directive name="name"></div>
         </div></div>

具体演示请看:http://kin-sample.coding.io/angular/directive/isolated-scope-at-interact.html

= 局部 scope 属性

= 通过 directive 的 attr 属性的值在局部 scope 的属性和父 scope 属性名之间建立双向绑定。
意思是,当你想要一个双向绑定的属性的时候,你可以使用=来引入外部属性。无论是改变父 scope 还是隔离 scope 里的属性,父 scope 和隔离 scope 都会同时更新属性值,因为它们是双向绑定的关系。

示例代码:

js 代码:

 app.controller("myController", function ($scope) {
        $scope.user = {
            name: 'hello',
            id: 1
        };
    }).directive("isolatedDirective", function () {
        return {
            scope: {
                user: "="
            },
            template: 'Say:{{user.name}} <br>改变隔离scope的name:<input type="buttom" value="" ng-model="user.name"/>'
        }
    })

html 代码:

<div ng-controller="myController">
    <div>父scope:
        <div>Say:{{user.name}}<br>改变父scope的name:<input type="text" value="" ng-model="user.name"/></div>
    </div>
    <div>隔离scope:
        <div isolated-directive user="user"></div>
    </div>
    <div>隔离scope(使用{{name}}):
        <div isolated-directive user="{{user}}"></div>
    </div>
</div>

具体演示请看:http://kin-sample.coding.io/angular/directive/isolated-scope-eq-interact.html

& 局部 scope 属性

& 方式提供一种途经是 directive 能在父 scope 的上下文中执行一个表达式。此表达式可以是一个 function。
比如当你写了一个 directive,当用户点击按钮时,directive 想要通知 controller,controller 无法知道 directive 中发生了什么,也许你可以通过使用 angular 中的 event 广播来做到,但是必须要在 controller 中增加一个事件监听方法。
最好的方法就是让 directive 可以通过一个父 scope 中的 function,当 directive 中有什么动作需要更新到父 scope 中的时候,可以在父 scope 上下文中执行一段代码或者一个函数。

如下示例在 directive 中执行父 scope 的 function。

js代码:

 app.controller("myController", function ($scope) {
        $scope.value = "hello world";
        $scope.click = function () {
            $scope.value = Math.random();
        };
    }).directive("isolatedDirective", function () {
        return {
            scope: {
                action: "&"
            },
            template: '<input type="button" value="在directive中执行父scope定义的方法" ng-click="action()"/>'
        }
    })

html 代码:

 <div  ng-controller="myController">
        <div>父scope:
            <div>Say:{{value}}</div>
        </div>
        <div>隔离scope:
            <div isolated-directive action="click()"></div>
        </div>
</div>

具体演示请看:http://kin-sample.coding.io/angular/directive/isolated-scope-ad-interact.html

使用小结

在了解 directive 的隔离 scope 跟外部环境交互的三种方式之后,写一些通用性的组件更加便捷和顺手。不再担心在 directive 中改变外部环境中的值,或者执行函数的重重困境了。
更多请参考API文档:https://docs.angularjs.org/api/ng/service/$compile 。
如有纰漏,请指正!

http://www.lbrq.cn/news/2785501.html

相关文章:

  • 富利建设集团有限公司网站千锋教育培训机构就业率
  • 优化后的网站百度seo优化教程免费
  • 网站左右箭头素材成人大学报名官网入口
  • 网站建设需要哪些准备网络推广的方式
  • 佛山大良网站建设招聘百度网盘电脑版
  • 汉阴网站建设排行榜百度
  • 黄石做企业网站搜狐综合小时报2022113011
  • 有了域名后怎么完成网站建设广告投放是做什么的
  • 公司简介范文100字左右自建站seo如何做
  • 越秀网站建设优化坚持
  • 格尔木市公司网站建设比较成功的网络营销案例
  • 博罗网站建设哪家好军事新闻
  • 通辽企业网站建设b站推广网站2023
  • 校园二手网站设计论文一句话让客户主动找你
  • 网站制作素材网站泉州百度网站推广
  • 济南做网站价格陕西seo优化
  • 常州网站制作费用郑州网站优化哪家好
  • 网站设计 开发人员手机网站智能建站
  • 花卉网站建设的总结与市场调研报告
  • 茶企业网站建设模板网络公司取什么名字好
  • 石狮住房和城乡建设局网站苏州网站建设书生
  • 手机网站图片做多大百度浏览器下载
  • 模板性公司网站图片中国足彩网竞彩推荐
  • wap自助建站排板合肥头条今日头条新闻最新消息
  • 做网站app的工资高吗个人购买链接
  • 聊城网站建设品牌促销方法100种
  • 湖南企业做网站广州今日头条新闻最新
  • 建个简单的网站广州seo网站服务公司
  • 做字幕的网站外贸公司一般怎么找客户
  • 网红网站建设官网seo简介
  • 嵌入式的各个要点总结(不断更新)
  • 双模式 RTMP H.265 播放器解析:从国内扩展到 Enhanced RTMP 标准的演进
  • 实现自己的AI视频监控系统-第一章-视频拉流与解码1
  • RocketMq消费者动态订阅topic
  • react-quill-new富文本编辑器工具栏上传、粘贴截图、拖拽图片将base64改上传服务器再显示
  • flink+clinkhouse安装部署