相同的参数传递给HTML而不是按钮(Same parameter got passed into HTML but not button)

编程入门 行业动态 更新时间:2024-10-25 12:20:12
相同的参数传递给HTML而不是按钮(Same parameter got passed into HTML but not button)

我想在angularjs中传递一个参数。 请注意{{dish.id}}出现在下面两次,一次正确通过,另一次失败。

html模板是

<ion-popover-view> <ion-content> <button class="button button-full button-light" ng-click="addFavorite({{dish.id}})"> Add to favorites: dish {{dish.id}} </button> <button class="button button-full button-light" ng-click="addComment()">Add comment</button> </ion-content> </ion-popover-view>

控制器中的功能是

$scope.addFavorite = function (index) { console.log("added from DishDetail: index is " + index); favoriteFactory.addToFavorites(index); console.log("finished"); }

然而,奇怪的是视图中的两个{{dish.id}},html中的一个被正确显示,而传递给fucntion的那个是“未定义的”

有人可以帮我解决这个问题吗? 有点被困在这里几天,无法弄清楚自己。

谢谢!

I am trying to pass a parameter in angularjs. Please note the {{dish.id}} appear twice below, one was correctly passed, the other failed.

the html template is

<ion-popover-view> <ion-content> <button class="button button-full button-light" ng-click="addFavorite({{dish.id}})"> Add to favorites: dish {{dish.id}} </button> <button class="button button-full button-light" ng-click="addComment()">Add comment</button> </ion-content> </ion-popover-view>

the function in the controller is

$scope.addFavorite = function (index) { console.log("added from DishDetail: index is " + index); favoriteFactory.addToFavorites(index); console.log("finished"); }

However, the weird thing is for the two {{dish.id}} in the view, the one in html was correctly displayed while the one to pass into the fucntion is "undefined"

Could someone kindly help me on resolving this? Kinda be stuck here for a few days and couldnt figure out myself.

Thanks!

最满意答案

摆脱以ng为前缀的html属性中的插值,这些属性由$ scope处理。$ apply()方法

<button class="button button-full button-light" ng-click="addFavorite(dish.id)">

Get rid of interpolation in ng- prefixed html attributes, those are handling by $scope.$apply() method

<button class="button button-full button-light" ng-click="addFavorite(dish.id)">

更多推荐

id,dish,html,中的,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-04-29 05:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   按钮   参数   HTML   button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!