选择列表项的angularjs指令是什么? [英] What is the angularjs directive to select list item?

查看:77
本文介绍了选择列表项的angularjs指令是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板列表,我想从列表中选择其中一个,并将id传递给表单内的输入。我的laout看起来像这样

i have a list of templates and i would like to select one of them from a list and pass the id to an input inside a form. My laout looks like this

<form>
    <input type="hidden" name="template_id" ng-model="template.template_id" />
    <input type="text" name="template_name" ng-model="template.template_name" />
    <ul>
        <li id="1">Template1</li>
        <li id="2">Another Template</li>
    </ul>
    <button type="submit"></button>
</form>

现在我希望当我按下任何一个< li> 项目,用于更改输入中的内容。可以使用指令完成吗?谢谢你,Daniel。

Now i would like when i press on any of the <li> items, to change the content from the inputs. Can this be done using a directive? Thank you, Daniel.

当我按下第一个列表项时,我希望输入的template_id和template_name设置为template_id = 1和template_name = Template1,当我按第二个列表项,将template_id设置为2,将template_name设置为另一个模板。

When i press on first list item, i want the input having the template_id and template_name to set to template_id = 1 and template_name = Template1 and when i press the second list item, to set template_id to 2 and template_name to Another template.

推荐答案

试试这个:


     <ul>
         <li ng-repeat="list in templateList" ng-click="setValue(list)">{{list.name}}</li>
     </ul>


在控制器中

  $scope.templateList = [{id:1, name: 'Template1'}, {id:2, name: 'Another Template'}]

  $scope.template = {};
  $scope.setValue = function(list) {
    $scope.template.template_id = list.id;
    $scope.template.template_name = list.name;
  }

SEE DEMO

这篇关于选择列表项的angularjs指令是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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