javascript 选择列表项的 angularjs 指令是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19397987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the angularjs directive to select list item?
提问by Pacuraru Daniel
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
我有一个模板列表,我想从列表中选择其中一个并将 id 传递给表单内的输入。我的布局看起来像这样
<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>
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.
现在我想当我按下任何<li>
项目时,更改输入中的内容。这可以使用指令来完成吗?谢谢你,丹尼尔。
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.
当我按下第一个列表项时,我希望将 template_id 和 template_name 的输入设置为 template_id = 1 和 template_name = Template1,当我按下第二个列表项时,将 template_id 设置为 2 并将 template_name 设置为另一个模板。
回答by Nitish Kumar
Try this:
试试这个:
in HTML :
在 HTML 中:
<ul> <li ng-repeat="list in templateList" ng-click="setValue(list)">{{list.name}}</li> </ul>
<ul> <li ng-repeat="list in templateList" ng-click="setValue(list)">{{list.name}}</li> </ul>
in Controller
在控制器中
$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
看演示