var app = angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', function ($scope) {
// Define $scope.telephone as an array
$scope.field = [];
$scope.value = [];
// Create a counter to keep track of the additional telephone inputs
$scope.inputCounter = 0;
}]);
// I've created this directive as an example of $compile in action.
app.directive('addInput', ['$compile', function ($compile) { // inject $compile service as dependency
return {
restrict: 'A',
link: function (scope, element, attrs) {
// click on the button to add new input field
element.find('button').bind('click', function () {
// I'm using Angular syntax. Using jQuery will have the same effect
// Create input element
var input = angular.element('<div><input type="text" ng-model="field[' + scope.inputCounter + ']"><input type="text" ng-model="value[' + scope.inputCounter + ']"></div>');
// Compile the HTML and assign to scope
var compile = $compile(input)(scope);
// Append input to div
element.append(input);
// Increment the counter for the next input to be added
scope.inputCounter++;
});
}
}
}]);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.