我使用ng-repeat
将对象加载到可点击的框中。
现在我想在用户点击其项目框时返回对象标题
通常我会把它放在ng-model
中但是因为我使用ng-repeat
模型将在每个创建的框中重复使用...
我正在寻找一种检查所选对象标题的方法。并将其放入ng模型中以便以后重复使用。
现在代码:
在html中创建对象框:
<div class="row form-group product-chooser">
<a href="#drop">
<div id="catagories" ng-repeat="catagories in main.catagories" class="col-xs-12 col-sm-12 col-md-4 col-lg-4" value="{{catagories.title}}">
<div class="product-chooser-item">
<img src="{{catagories.image}}" class="img-rounded col-xs-4 col-sm-4 col-md-12 col-lg-12" alt="Catagories">
<div class="col-xs-8 col-sm-8 col-md-12 col-lg-12">
<span class="title">{{catagories.title}}</span>
<span class="description">{{catagories.description}}</span>
<input type="radio" name="product" value="{{catagories.title}}">
</div>
<div class="clear"></div>
</div>
</div>
</a>
</div>
<P>selected is: {{main.CreateProject.ProjectCatagorie}}</P>
contoller js file:
// objects
vm.catagories = [
{
id: 1,
title: 'Mobile and Desktop',
description: 'Full concept design to complete App',
image: '../img/Project/desktop_mobile.png'
},
{
id: 2,
title: 'Desktop',
description: 'Blogs, Webshops, complete full back-end Web-Apps',
image: '../img/Project/desktop.png'
},
{
id: 3,
title: 'Mobile',
description: 'Mobile websites, Apps for Android and/or IOS',
image: '../img/Project/mobile.png'
}
];
//javascript to put the checked prop on the checked box:
//works
$(function(){
$('div.product-chooser').find('div.product-chooser-item').on('click', function(){
// remove selected class form all
$(this).parent().parent().find('div.product-chooser-item').removeClass('selected');
// put checked
$(this).addClass('selected');
$(this).find('input[type="radio"]').prop("checked", true);
// get seleciton
if ($('input[type=radio]:checked').length > 0) {
// do something here
alert($('input[type=radio]:checked').val());
}
// set rest disabled
$('div.product-chooser').not('.selected').addClass('disabled');
});
});
vm.CreateProject = function(){
// selected catagorie
//fails
ProjectCatagorie = $('input[type=radio]:checked').val();
// this model is in the function: CreateProject within the maincontroller.
// function doesnt end here...
答案 0 :(得分:1)
您可以在每个复选框上添加ngClick指令,并直接设置main.CreateProject.ProjectCatagorie
:
<input type="radio" name="product" ng-click="main.CreateProject.ProjectCatagorie = catagories">
答案 1 :(得分:0)
ng-repeat确实会创建一个新的子范围。诀窍是将ng-model值包装在父作用域中定义的对象中,一切都按预期工作。
<div ng-repeat="catagory in catagories">
<input type="radio" ng-model="selection.catagory" name="product" ng-value="catagory">
</div>
function myCtrl($scope) {
$scope.selection = {catagory : null};
$scope.catagories = [
{
id: 1,
title: 'Mobile and Desktop',
},
{
id: 2,
title: 'Desktop',
},
{
id: 3,
title: 'Mobile',
}
];
}