Skip to content

Instantly share code, notes, and snippets.

@johan--
Forked from commercial-hippie/angular_test.html
Created October 30, 2018 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan--/fee884099f2667413ed46860dd1aa29a to your computer and use it in GitHub Desktop.
Save johan--/fee884099f2667413ed46860dd1aa29a to your computer and use it in GitHub Desktop.
AngularJS 101: Trying to figure out how to make clone-able or duplicate-able form inputs and input groups.
<!DOCTYPE html>
<html ng-app="sasApp">
<head>
<title>AngularJS Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
</head>
<body>
<div class="container">
<h1>AngularJS</h1>
<div ng-controller="MultipleFieldCtrl" ng-init="name = 'Value[block]'">
<div ng-repeat="fieldGroup in fieldGroups" class="well">
<div class="form-group">
<label for="">Text</label>
<input ng-model="fieldGroup.text" name="{{ getName($index) }}" type="text" class="form-control">
</div>
<div class="form-group">
<label for="">Href</label>
<input ng-model="fieldGroup.href" name="{{ getName($index) }}" type="text" class="form-control">
</div>
<div class="form-group">
<label for="">Color</label>
<select ng-model="fieldGroup.select" name="{{ getName($index) }}" type="text" class="form-control">
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="red">Red</option>
</select>
</div>
<button ng-click="delete(fieldGroup)" class="btn btn-danger btn-sm">Remove</button>
</div>
<button ng-click="clone()" class="btn btn-primary">Clone</button>
</div>
</div>
<script type="text/javascript">
var sasApp = angular.module('sasApp', []);
sasApp.controller('MultipleFieldCtrl', function ($scope) {
$scope.fieldGroups = [
{
'text': 'value 1',
'href': 'value 1',
'select': 'green'
},
{
'text': 'value 2',
'href': 'value 2',
'select': 'red'
}
];
$scope.getName = function ( $index) {
return $scope.name + '[' + $index + ']';
}
$scope.clone = function() {
$scope.fieldGroups.push({});
}
$scope.delete = function(fieldGroup) {
var index = $scope.fieldGroups.indexOf(fieldGroup);
$scope.fieldGroups.splice(index, 1);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment