4

我想将链接 href 属性绑定到控制器中的变量,但我也希望将该 url 绑定到变量。我想使用内置绑定来完成此操作,而无需手动查看更改并重新加载 url。这可能吗?

// In the controller
$scope.section = 'section1';
$scope.page = 'page1';
$scope.url = 'http://myurl/{{section}}/{{page}}';


<!-- In the template -->
<a ng-href="{{url}}">Page Link</a>

这是我实际代码的简化。在模板中声明 url 模式会起作用,但我需要在传入的字符串中定义 url。

4

2 回答 2

6

只需设置ng-href

<a ng-href="http://myurl/{{section}}/{{page}}">Page Link</a>
于 2015-01-22T19:15:24.400 回答
2

在控制器中,您不需要使用花括号表达式。

替换这个:

$scope.url = 'http://myurl/{{section}}/{{page}}';

有了这个:

$scope.url = 'http://myurl/'+$scope.section+'/'+$scope.page;

并将其绑定到您的模板中,请使用:

<a ng-href="http://myurl/{{section}}/{{page}}">Page Link</a>

因此,您现在可以观察任何变化。

于 2015-01-22T19:14:58.263 回答