传递路径在angularjs指令 [英] passing paths in an angularjs directive

查看:153
本文介绍了传递路径在angularjs指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想传递的路径的指令

I have a directive which i'd like to pass a path

<mydirective api="/my/tomcat/server?asd=123&efg=456">

不过,我得到词法错误:意外下一个字符。我认为一些编码需要发生。可有人建议吗?

However I get "Lexer Error: Unexpected next character". I assume some encoding needs to take place. Can someone advice?

推荐答案

我不知道你为什么没有看到你的code,如果你能更新后得到词法错误,我们也许能够告诉为什么出现这种情况。在此期间,也有一些方法来检索您的指令的属性值。并非所有的将一个字符串文字工作。

I'm not sure why you are getting the lexer error without seeing your code, if you could update the post, we may be able to tell why that happens. In the meantime, there are a few ways to retrieve an attribute value in your directive. Not all will work with a string literal.

1) @ 隔离范围结合:此分析的价值和将工作与你的HTML。解析其实后来会发生那么这个值将不会在该指令的内部方法立即可用(即链接等),并在指令中的模板方法。例如:

1) The @ isolate scope binding: This parses the value and will work as is with your HTML. The parse actually happens later so this value will not be immediately available in the directive's internal methods (i.e. link, etc.) and is best used in the directive's template method. Ex.:

scope: {
    api: '@'
}

2) = 隔离范围结合:这将工作,如果你换你的前任pression在单引号,因为它计算属性为前pression。例如:

2) The = isolate scope binding: This will work if you wrap your expression in single quotes because it evaluates the attribute as an expression. Ex.:

scope: {
    api: '='
}

和在HTML(注意单引号):

And in your HTML (notice the single quotes):

<mydirective api="'/my/tomcat/server?asd=123&efg=456'">

3)属性评估:这可以让你直接从该指令的内部方法评估该属性的字符串值,将工作与你的HTML。例如:

3) Attribute Evaluation: This allows you to evaluate the attribute string value directly from the directive's internal methods and will work as is with your HTML. Ex:

link: function(scope,element,attrs){
    console.log(attrs.api);
}

您可以阅读更多有关在这里的 AngularJS指令文档的指令。

You can read more about directives in the AngularJS directive doc here.

这篇关于传递路径在angularjs指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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