I'm a newbie to Angular. Enjoy the fun of learning new things currently.
Here is my intention - I would like to have a list of input boxes. And then I want to get the combined string value from user input. The count of input boxes is passed from the custom directive's parent controller. After I get the combined value I also need to pass it back to its parent scope.
Here is where I am so far.
angular.module('starter.directives', [])
.directive('userInput', function($compile) {
return {
// templateUrl: 'templates/user-input-singlebox.html',
template: '<div></div>',
restrict: 'E',
scope: {
records: '='
},
//controller needs to serve as API
controller: function($scope) {
},
link: function($scope, $element, $attrs) {
$scope.inputCount = $scope.$parent.userInputStaticCount;
var records = [];
$scope.getInputBoxNum = function() {
return new Array($scope.inputCount);
};
$scope.userInputBoxRecordList = "hello";
$scope.getUserInput = function() {
//return a combined string of value from input box index 0 to input box index n-1;
}
var html = '<div ng-repeat="i in getInputBoxNum() track by $index"><input id="{{$index+1}}" type="text" ng-model="userInputBoxRecordList" name="box"' + 'style="background-color:beige; border:1px solid black;"></div>';
var e = $compile(html)($scope);
$element.replaceWith(e);
}
}
});
- However, I'm assigning a unique id to each input box, I wonder if there is a better way like using an ng-model to get the userInput() function work.
- I want to dynamically generate a list of user input boxes. Currently I put all the code in post link function. I believe I can write ONE input box in a template and then generate a list of boxes. However didn't figure it out by self. Any hints?