0

I have this simple directive:

app.directive('string', function () {
    return{
        template: '<input id="{{field.name}}" name="{{field.name}}" type="text" value="{{field.value}}"/>',
        restrict: 'E',
    };
});

That I'm creating in a controller:

for(var i=0;i<$scope.steps;i++){
   var step = $scope.steps[i];
   var element = document.createElement(step.type);

   var compiled = $compile(element)($scope);
   $(document.body).append(compiled);
}

this outputs a textfield without a value. How can I give my directive the 'step' variable and print it out in the text field as step.value?

1 Answer 1

1

You should simply use ng-repeat for this

<string ng-repeat='field in steps'></string>

Update: Doing what ng-repeat does

for(var i=0;i<$scope.steps;i++){
   var childScope=$scope.$new();
   childScope.field=$scope.steps[i];
   var element = document.createElement(field.type);

   var compiled = $compile(element)(childScope);
   $(document.body).append(compiled);
}
Sign up to request clarification or add additional context in comments.

8 Comments

But not all fields in steps are strings :)
use it with filter 'field in steps|filter:{type:"string"}'
Or you can even use <string ng-repeat='field in steps' ng-if='field.type=="string"'></string>
but what I want is to retain the order in which they are in the array, I'd feel stupid writing <string> and then <option> and then <whatever> for each different type. I want to do it programmatically.
Surely what I'm asking above is possible?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.