I need to be able to display inputs based on an array of objects that describe them. For example, this describes 2 inputs foo and bar:
$scope.z.inputs = [{
name: "foo",
hint: "Enter foo number.", // if hint is specified, it should be in a tooltip
min: "2", // this is optional
max: "100" // this is optional
}, {
name: "bar"
}];
See plunkr: http://plnkr.co/edit/giWusNpiRhHibJi3hr2e?p=preview It has a crude directive that works with static inputs. It can't handle the other inputs (foo and bar). Comments above the directive describe the functionality it provides.
What approach can be done to handle these inputs while keeping the extra functionality the directive provides?
What I tried...
Setting min="{{in.min}}" on the input element results in the value in the linking function being the string "{{in.min}}". I tried setting scope and using @ (see the answer at What is the difference between '@' and '=' in directive scope in AngularJS?). However, I didn't understand how to "use attr.$observe..." as is mentioned. And what I did try didnt work. Plus the variable bound to the input with ng-model didn't get updated anymore after setting the directive's scope.
After reading about the ng-switch approach mentioned at Angularjs dynamic directives I tried ng-switch and ng-if. I kept a ng-if for min in the plunkr. If I can avoid having to use ngswitch or ngif for things like min, max, and hint that would be helpful as it would be unwieldy with more attributes.
Update
In the plnkr, I got min and max to work using ng-if statements to handle all possible combinations. I needed to modify the check in the directive for min and max. Instead of checking whether a number is specified, just check whether the attribute is specified. If it is (even if an expresion), then leave it alone and it gets updated with the right value.
I'd still like to be able to avoid the proliferation of ng-ifs if there's a way to do that.