在NG-点击角插值范围变量 [英] Angular interpolate scope variable in ng-click

查看:105
本文介绍了在NG-点击角插值范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构建以ng单击范围变量的名称。
例如在我的地方是动态生成的名称在NG-重复的形式。
我使用的X编辑插件,在这种情况下,这样的名称将创建一个名为范围的变量{{box.key}} {{$指数}}

 < D​​IV NG重复=课程box.value轨道由$指数>
 <形成可编辑的格式名称={{box.key}} {{$指数}}>

的问题是如何动态地访问的变量。该按钮也是在NG-repeate DIV。

 <按钮类=BTN BTN-主要NG点击={{box.key}} {{$指数}} $显示()。>编辑&LT ; /按钮>


解决方案

您形式纳克重复,它定义了每格重复一个孩子域内声明。因此,为了确保形式是从CONTROLER范围访问,不仅从孩子NG重复范围时,请确保您的控制器定义的形式一个空对象(或数组):

  $ scope.forms = {};

和的形式生成的名字看起来像是 forms.form1 。例如:

 <形式{{box.key}} {{$指数}}的形式编辑的格式名称=>

然后在您的视图:

  NG-点击=showForm(box.key,$指数)

和在你的控制器:

  $ scope.showForm =功能(键,索引){
    VAR表格名称=键+指数
    。$ scope.forms [表格名称] $显示();
}

How do I construct the name of a scope variable in ng-click. E.g the I have a form in ng-repeat where the name is generated dynamically. I am using x-editable plugin so name in this case will create a scope variable called {{box.key}}{{$index}}

<div ng-repeat="course in box.value track by $index">    
 <form editable-form name="{{box.key}}{{$index}}">

The question is how to access the variable dynamically. The button is also in the ng-repeate div.

<button class="btn btn-primary" ng-click="{{box.key}}{{$index}}.$show()">Edit</button>

解决方案

Your forms are declared inside ng-repeat, which defines one child scope per repeated div. So, to make sure the forms are accessible from the controler scope and not only from the child ng-repeat scope, make sure your controller defines an empty object (or array) of forms:

$scope.forms = {}; 

and that the generated name of the forms looks like forms.form1. For example:

<form editable-form name="forms.{{box.key}}{{$index}}">    

Then in your view:

ng-click="showForm(box.key, $index)"

and in your controller:

$scope.showForm = function(key, index) {
    var formName = key + index
    $scope.forms[formName].$show();
}

这篇关于在NG-点击角插值范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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