Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 为什么指令ng href需要{{}},而其他指令不需要';T_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs 为什么指令ng href需要{{}},而其他指令不需要';T

Angularjs 为什么指令ng href需要{{}},而其他指令不需要';T,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我只是想知道为什么我需要为ng href添加双大括号,而其他一些指令不需要它们 <a ng-href="{{myScopeVar}}" ng-if="myScopeVar">link</a> 请注意,ng href需要大括号,而ng如果不需要大括号。我不太确定您的问题。但我想你可能想知道为什么角度指令中有不同的语法风格。首先,请看一下这篇文章:答案解释了{{},{}和无大括号之间的区别 对于您的具体示例,如文档中所述:ng href需要一个模板(可以包含{}}标记的

我只是想知道为什么我需要为ng href添加双大括号,而其他一些指令不需要它们

<a ng-href="{{myScopeVar}}" ng-if="myScopeVar">link</a>


请注意,
ng href
需要大括号,而
ng如果不需要大括号。

我不太确定您的问题。但我想你可能想知道为什么角度指令中有不同的语法风格。首先,请看一下这篇文章:答案解释了
{{}
{}
和无大括号之间的区别

对于您的具体示例,如文档中所述:
ng href
需要一个模板(可以包含
{}}
标记的任何字符串),而ng if需要一个表达式-例如,您可以不编写
{}
,因为angular会对其求值

如果您查看一下您将看到的角度源,
ng href
使用
attr.$observe
,而ng If使用
$scope.$watch
功能$每次更改属性值时,都会对“观察”进行缩放$当表达式结果为新值时,将调用watch

但为什么这两种方式不同呢?我认为其中一点是更易于使用和代码可读性。现在你可以写:

<a ng-href="http://yourdomain.com/users/{{userId}}/post/{{postId}}">title</a>

即使Iuse
ng href
data ng href
也会抛出验证错误。 为了通过验证错误,我使用了


希望它能帮助别人

假设你想动态构建url,例如:
ng href=“#/foobar/{{barfoo}}”
。另外,您可能需要阅读中的说明以获得更详细的解释。我是否可以使用函数输出作为
ng href
的对象,例如
ng href=“{{my_function()}”
?@ShellFish是的,您可以
<a ng-href="'http://yourdomain.com/users/'+userId+'/post'+postId">title</a>