首页 \ 问答 \ 你能否在Angular 2中拥有独立指令?(Can you have a stand alone directive in Angular 2)

你能否在Angular 2中拥有独立指令?(Can you have a stand alone directive in Angular 2)

我正在玩Angular 2,我想做的是将一个独立的指令直接分配给HTML中的元素。 这可能吗?

指示:

import {Directive, ElementRef} from 'angular2/core';
@Directive({
    selector: '[Slidernav]'
})
export class Slidernav {
    constructor(private element: ElementRef) {
        console.log('Slider Run');
    }
}

我会将它分配给任何HTML元素,如下所示:

<div slidernav>Some content</div>

我问的原因是因为我不想使用Component在DOM中已经存在的HTML设置模板。

编辑

因此,基于响应,这是组件

组件滑块

 import {Component, View, ElementRef} from 'angular2/core';
 @Component({
    selector: 'Slider',
    template: '<ng-content></ng-content>'
 })
 export class Slider {
     constructor(private element: ElementRef) {
         console.log('Slider');
     }
}

的index.html

<Slider>
    <div>hello world</div>
</Slider>

我希望Div能够在Slider中保留内容hello世界。 AFAIK,它应该如何工作基于我一直在阅读有关Transclusion的内容。 但控制台错误是:

Error: The component Slider has 1 <ng-content> elements, but only 0 slots were provided.

I'm playing around with Angular 2, What I'd like to do is to have a stand alone directive directly assigned to an element within the HTML. Is this possible?

Directive:

import {Directive, ElementRef} from 'angular2/core';
@Directive({
    selector: '[Slidernav]'
})
export class Slidernav {
    constructor(private element: ElementRef) {
        console.log('Slider Run');
    }
}

and I would assign it to any HTML element like so:

<div slidernav>Some content</div>

The reason why I'm asking is because I don't want to set the template with HTML that already exists in the DOM using a Component.

EDIT

So based on the responses, this is the component

Component Slider

 import {Component, View, ElementRef} from 'angular2/core';
 @Component({
    selector: 'Slider',
    template: '<ng-content></ng-content>'
 })
 export class Slider {
     constructor(private element: ElementRef) {
         console.log('Slider');
     }
}

index.html

<Slider>
    <div>hello world</div>
</Slider>

I would be expecting the Div to remain within Slider with the content hello world. AFAIK that's how it should work based on what i've been reading about Transclusion. But the console error is:

Error: The component Slider has 1 <ng-content> elements, but only 0 slots were provided.

原文:https://stackoverflow.com/questions/35274885
更新时间:2022-03-02 11:03

最满意答案

boot2docker ssh "tce-load -w -i python.tcz"也可以解决这个问题(你需要互联网;-))对于docker和ansible你需要“docker-py”

  1. 设置点,登录boot2docker
  2. wget https://bootstrap.pypa.io/get-pip.py
  3. python get-pip.py
  4. pip install docker-py

还会添加到您的库存文件中:

dockerhost ansible_connection=ssh ansible_ssh_host=192.168.59.103 ansible_ssh_user=docker ansible_ssh_private_key_file=~/.ssh/id_boot2docker ansible_python_interpreter=/usr/local/bin/python

The solution was simple: Python isn't installed by default by Boot2Docker.

To install, run

$ boot2docker ssh "wget http://www.tinycorelinux.net/6.x/x86/tcz/python.tcz && tce-load -i python.tcz && rm -f python.tcz"

I created a script to do this automatically, see https://gist.github.com/bcattle/90e64fbe808b3409ec2f

相关问答

更多
  • 在创建容器是加上-p 端口号 如:docker run -d -it -p 8080:8080 -p 8081:8081 --name xxxx ...... 就是将创建的容器的8080和8081 端口映射物理机的8080和8081端口,这里映射了2个端口
  • 您将需要使用新的内存设置重新初始化boot2docker虚拟机: $ boot2docker delete $ boot2docker init -m 5555 ... lots of output ... $ boot2docker info { ... "Memory":5555 ...} 您现在可以boot2docker up并且映像将始终使用配置的内存量。 You will need to re-initialize the boot2docker VM with the new memory s ...
  • 显然,boot2docker上的Google托管虚拟机存在一些问题。 我找到了一个适用于我的Ubuntu 14.04机器以及我的Windows 8.1机器的解决方案。 可以在此处找到分步指南: https : //gist.github.com/xuru/604da3c02ec96904d352 最关键的部分似乎是某个版本图像的下载: boot2docker download --iso-url=https://github.com/boot2docker/boot2docker/releases/down ...
  • 解决方案/解决方法是使用用于运行VM的软件来保存VM的状态(在boot2docker是VirtualBox的情况下)。 重新启动后,您只需加载已保存的状态,事情就会像以前一样继续工作。 The solution/workaround is to save the state of the VM using the software used to run it (in the case of boot2docker that is VirtualBox). After a restart you can t ...
  • ssh到boot2docker是使用该工具的常用方法:你ssh到boot2docker vm。 打开ssh会话后,您可以检查容器以列出每个容器的卷 。 docker inspect -f '{{ .Name }},{{ range $key, $value := .Volumes }}{{ $key }},{{ $value }}##~#{{ end }}' 注意:自.Mounts 1.8以来,检查卷现在使用.Mounts 。 请参阅“ 如何在docker容器中列出卷? ”。 ...
  • 请参阅“ 如何让Docker在公司防火墙后的Windows系统上运行? ” 你需要设置 在cmd会话中设置HTTP_PROXY , HTTPS_PROXY和NO_PROXY ,以便boot2docker download工作。 (导出) /var/lib/boot2docker/profile中的http_proxy , http_proxy和no_proxy ,以便执行/var/lib/boot2docker/profile docker search (例如)。 export http_proxy=ht ...
  • 经过大量测试后,我决定使用NFS(网络文件系统)而不是rsync,这使我可以同步我的机器和Docker主机。 而且效果很好。 这种配置允许我管理它。 #sync local files to the docker host VM config.vm.synced_folder "../", "/vagrant", type: "nfs", mount_options: ["nolock", "vers=3", "udp"], id: "nfs-sync" 参考: https : //github.com/ ...
  • 这意味着你的$PATH (在boot2docker会话中)以某种方式在 /bin 之前包含“ C:/Program Files (x86)/Git/bin/ ”。 检查docker run --rm -it ubuntu:latest时的$PATH docker run --rm -it ubuntu:latest 。 您设法在boot2docker ssh会话中运行映像,但boot2docker ssh docker ps似乎没有显示任何预期。 只有一个docker run --rm -it ubuntu ...
  • 我怀疑你的容器没有启动,因为当机器(boot2docker VM)重新启动时,它仍然处于' Exited '状态。 用docker-machine ssh default "docker ps -a" 。 (在docker-machine start default ) 如果是这样,请在/mnt/sda1/var/lib/boot2docker/profile一个对脚本的调用,该脚本将: 测试容器是否退出 : docker inspect -f {{.State.Running}} $CONTAINER_I ...
  • boot2docker ssh "tce-load -w -i python.tcz"也可以解决这个问题(你需要互联网;-))对于docker和ansible你需要“docker-py” 设置点,登录boot2docker wget https://bootstrap.pypa.io/get-pip.py python get-pip.py pip install docker-py 还会添加到您的库存文件中: dockerhost ansible_connection=ssh ansible_ssh_ho ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)