Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.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-您能在javascript块中引用一个repeater变量吗?_Javascript_Angularjs - Fatal编程技术网

AngularJS-您能在javascript块中引用一个repeater变量吗?

AngularJS-您能在javascript块中引用一个repeater变量吗?,javascript,angularjs,Javascript,Angularjs,简单地说,我正在使用AngularJS尝试做类似的事情: <ul> <li ng-repeat="thing in things"> <a id="mybutton" href="link{{thing.Id}}">Click</a> <script type="text/javascript"> $("#mybutton").click(function() { alert(

简单地说,我正在使用AngularJS尝试做类似的事情:

<ul>
  <li ng-repeat="thing in things">
    <a id="mybutton" href="link{{thing.Id}}">Click</a>
    <script type="text/javascript">
      $("#mybutton").click(function()
      {
        alert("Id: " + {{thing.Id}});
      });
    </script>
  </li>
</ul>
  • $(“#我的按钮”)。单击(函数() { 警报(“Id:”+{{thing.Id}}); });
两个都是,还有警报(“Id:+thing.Id”);产生错误(未检查的标记“{”和“thing”未分别定义)。是否有方法可以从转发器范围内的javascript块访问thing.Id


为了增加一个实际的原因,我想使用该值和javascript中的其他一些东西(我可以访问jquery)构建并重定向到一个URL。

我会将它存储到类似“data id={{thing.id}”的元素中


然后从javascript中使用数据属性读取ID。

Mike,在标记中使用角度表达式({{thing.ID}})将不起作用,因为angular.js不是一个基于文本的模板系统,而是使用DOM作为其模板。有关此主题的详细信息如下:

现在,回到您的问题:您尝试在angular.js中执行的操作可以非常轻松地完成,而无需使用jQuery,下面是一个工作的JSFIDLE:

诀窍是在href上使用ng单击:

<a ng-click="click(thing.id)">Click</a>

在控制器的功能中,您可以使用jQuery指令,但最好使用angular的$location服务:

注意,文档建议使用ng href而不是href:Great-answer-pkozlowski。To-OP:我很抱歉为您提供了一个甚至都不正确的解决方案。我回答关于一个我没有使用过的模板引擎。
$scope.click = function(thingId) {
    alert("Id: " + thingId);
    //$location.path('/some/path/'+thingId);
}