我是使用AngularJS的新手,所以我不太清楚实现目标的最佳方法是什么。我想要做的是在我的hmtl中有一个输入标签的网格,其中type = number并设置它,以便每当值递增时,新对象将附加到列表中。同样,当它递减(它不能低于0)时,从列表中删除该类型的对象。
使用此代码,每当我增加输入框时,{}}会在底部显示该更改。但是,我不清楚我实际上是在绑定什么。每当用户递增输入框时,我不知道如何制作新的foo1对象。
这是我的代码:
var app = angular.module('FooTools', ['ionic'])
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
app.controller('fooCtrl', function($scope) {
//I want to bind foo objects to these lists
$scope.foo1 = [];
$scope.foo2 = [];
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/foo.js"></script>
</head>
<body ng-app="FooTools">
<ion-pane>
<ion-header-bar class="bar-positive">
<h1 class="title"><b>Foo Tools</b></h1>
</ion-header-bar>
<ion-content>
<div class="main-div">
<div ng-controller="fooCtrl"><br>
<div class="row">
<div class="col">
Column 1:
</div>
<div class="col">
Column 2:
</div>
</div>
<div class="row">
<div class="col">
<label class="item-input">
object1: <input type="number" ng-model="foo1"><br>
</label>
</div>
<div class="col">
<label class="item-input">
object2: <input type="number" ng-model="foo2"><br>
</label>
</div>
</div>
Foo: {{foo1 + " foo1, " + foo2 + " foo2"}}
</div>
</div>
</ion-content>
</ion-pane>
</body>
</html>
另外,这是我的foo1对象构造函数,如果有帮助:
function unit(value) {
this.value = value;
}
有谁知道如何做到这一点或更好地了解如何实现这一目标? (并不是说这有所不同,但这是一个离子项目。)
答案 0 :(得分:1)
如何绑定&#34; ng-change&#34;输入?例如:
<input type="number" ng-change="checkValues("foo1", fooN1)" ng-model="fooN1">
<input type="number" ng-change="checkValues("foo2", fooN2)" ng-model="fooN2">
在你的控制器里面
$scope.checkValues = function (a, b) {
if (a in $scope) {
if ($scope[a].length < b) { // If length is smaller that the number
$scope[a].push({}); //Adds the new object to the array
} else {
//Removes the last element of the array
$scope[a].pop();
}
}
}
希望这有帮助
答案 1 :(得分:0)
我不确定我是否完全理解你在这里要完成的任务,但请尝试以下修复(基于我迄今所理解的内容):
//in your DOM
<label class="item-input">
object1: <input type="number" ng-model="data.foo1">
<button ng-click="addToList(data.foo1)"> + </button>
<button ng-click="removeFromList(data.foo1)"> - </button>
</label>
//in your controller
//initialize data
$scope.data=[{foo1:0, foo2:0}];
//func to add obj to list when the value incremented
$scope.addToList = function (obj){
$scope.data.foo1 ++;
$scope.foo1List.push(obj);//add to your foo1 list
}
$scope.removeFromList = function (obj){
$scope.data.foo1 --;
// you can use $scope.foo1List.splice to remove from your array list
}
希望这有帮助!