AngularJS-如何在ng-switch中访问绑定值 [英] AngularJS - How to access a binding value within ng-switch

查看:51
本文介绍了AngularJS-如何在ng-switch中访问绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ng-repeat内,我有一个ng-switch条件.我希望ng-switch参数的值是一个绑定.但是,花括号绑定似乎没有转换为预期的生成值.我知道ng-switch会创建一个新的作用域,这肯定是造成问题的原因.我知道点"解决方法,但是这里的值来自通过控制器查询的服务,我无法弄清楚在这种情况下如何实现它.

Within a ng-repeat I have a ng-switch conditional. I want the value of the ng-switch argument to be a binding. However the curly-bracket binding doesn't seem to be converted into the expected generated value. I'm aware that ng-switch creates a new scope, and that is surely causing the problem. I know about a 'dot' work-around but as the values here are coming from a service via a controller query, I can't figure out how to implement it in this situation.

html

<tr ng-repeat="user in users">
    <td ng-switch on="editState"><p ng-switch-when="noEdit">{{user.username}}</p><input type="text" value="{{user.username}}" ng-switch-when="{{user.username}}"></td>
</tr>

controller.js

$scope.users = Users.query();

-编辑-

这是触发开关的功能

$scope.editUser = function(usernameEdit) {
    $scope.editState = usernameEdit;
};

推荐答案

如果您希望能够同时编辑多个用户,则单个$scope.editState将不起作用.我建议在每个user上放置一个edit属性,并使用该属性控制ng-switch:

A single $scope.editState won't work if you want to be able to edit multiple users at the same time. I suggest putting an edit property on each user, and use that property to control the ng-switch:

<tr ng-repeat="user in users">
   <td ng-switch on="user.edit"> 
      <span ng-switch-when="true">
         <input  type="text" value="{{user.username}}">
         <a href="" ng-click="user.edit=false">done</a>
      </span>
      <p ng-switch-default>
         <a href="" ng-click="user.edit=true">edit</a> {{user.username}}
      </p>
   </td>
</tr>

小提琴

这篇关于AngularJS-如何在ng-switch中访问绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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