加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJs将复杂数据传递到指令

发布时间:2020-12-17 09:01:30 所属栏目:安全 来源:网络整理
导读:我有以下指令: div teamspeak details="{{data.details}}"/div 这是对象结构: data: { details: { serverName: { type: 'text',value: 'my server name' },port: { type: 'number',value: 'my port' },nickname: { type: 'text' },password: { type: 'pass
我有以下指令:
<div teamspeak details="{{data.details}}"></div>

这是对象结构:

data: {
                details: {
                    serverName: { type: 'text',value: 'my server name' },port: { type: 'number',value: 'my port' },nickname: { type: 'text' },password: { type: 'password' },channel: { type: 'text' },channelPassword: { type: 'password' },autoBookmarkAdd: { type: 'checkbox' }
                }
}

并且我想它基于data.details对象内的数据生成链接。
不幸的是它不工作,因为我不能访问任何内部的细节对象的值,但如果我传递一个简单的数据结构,如:

<div teamspeak details="{{data.details.serverName.value}}"></div>

我可以使用{{details}}访问它。

这是我的指令代码:

App.directive('teamspeak',function () {
    return {
        restrict: 'A',template: "<a href='ts3server://{{details.serverName.value}}:{{details.port.value}}'>Teamspeak Server</a>",scope: {
            details: '@details',},link: function (scope,element,attrs) {
        }
    };
});

谢谢

阅读 Angularjs official site说明:

@ or @attr – bind a local scope property to the value of DOM
attribute. The result is always a string since DOM attributes are
strings. If no attr name is specified then the attribute name is
assumed to be the same as the local name. Given and widget definition of scope: { localName:’@myAttr’ },
then widget scope property localName will reflect the interpolated
value of hello {{name}}. As the name attribute changes so will the
localName property on the widget scope. The name is read from the
parent scope (not component scope).

所以你可以只发送一个字符串,传递一个对象,你需要设置一个双向的绑定使用=。

scope: {
        details: '=',

和你的HTML将看起来像

<div teamspeak details="data.details"></div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读