将范围变量连接到angular指令表达式中的字符串 [英] concat scope variables into string in angular directive expression

查看:70
本文介绍了将范围变量连接到angular指令表达式中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在角度ng-click指令中使用范围方法,如下所示:

I am using a scope method in an angular ng-click directive like so:

<a ng-click="$navigate.go('#/path/obj.val1/obj.val2')">{{obj.val1}}, {{obj.val2}}</a>

这里的麻烦是obj.val1和obj.val2被解释为传递给的字符串的一部分表达式中的方法。我需要将它们作为它们的变量进行评估......我不确定这里的正确方法是什么。

The trouble here is that obj.val1 and obj.val2 are interpreted as part of the string passed to the method in the expression. I need them to be evaluated as the variables that they are... I'm not sure what the right approach is here.

是否有一种角度方式来连接这些值到该字符串/表达式?

Is there an angular way to concat these values into that string/expression?

我只是做错了?

推荐答案

目前还不是很清楚问题是什么,以及你想从你发布的代码中完成什么,但我会对它进行一次尝试。

It's not very clear what the problem is and what you are trying to accomplish from the code you posted, but I'll take a stab at it.

一般来说,我建议在ng-click上调用一个函数,如下所示:

In general, I suggest calling a function on ng-click like so:

<a ng-click="navigateToPath()">click me</a>

obj.val1 & obj.val2 应该在您的控制器的$ scope上可用,您不需要将它们从标记传递到函数中。

obj.val1 & obj.val2 should be available on your controller's $scope, you dont need to pass those into a function from the markup.

然后,在你的控制器中:

then, in your controller:

$scope.navigateToPath = function(){
   var path = '/somePath/' + $scope.obj.val1 + '/' + $scope.obj.val2; //dont need the '#'
   $location.path(path)       
}

这篇关于将范围变量连接到angular指令表达式中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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