Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a situation where I receive a json object and within json data is a section that look something like this

{
  "elName": "elCst3",
  "label": "Label 3",
  "type": "input",
  "content": "some text",
}

This is assigned to $scope.data

The type expression in the object can be input or textarea

In my html template, depending on which value the TYPE is I am supposed to load either a textfield (input/text) or textarea element.

Pseudo logic of it is something like this..

IF {{data.type}} == "input" then

    <input id="data.elName" type="text" value="{{data.content}}">

ELSE IF {{data.type}} == "textarea" then

    <textarea id="data.elName">{{data.content}}</textarea>
ENDIF

How could I in the best and simplest way approach this in Angularjs?

Thanks!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I would use ng-if in your case. Something like:

HTML

<label ng-repeat="val in list">
    <div ng-if="val.type == 'input'">
        <input id="data.elName" type="text" value="{{val.content}}"></input>
    </div>
    <div ng-if="val.type == 'textarea'">
        <textarea id="data.elName">{{val.content}}</textarea>
    </div>
</label>

JS

 $scope.list = [{
        "elName": "elCst3",
            "label": "Label 3",
            "type": "input",
            "content": "some text for input"
    }, {
        "elName": "elCst4",
            "label": "Label 3",
            "type": "textarea",
            "content": "some text for textarea"
    }];

Demo Fiddle

share|improve this answer
    
I'll try this out and come back to comment. thanks! Could you just hint at what "fessmodule.$inject = ['$scope'];" does? –  ng-js learning curve Dec 14 '13 at 6:34
    
This worked great, thanks. –  ng-js learning curve Dec 14 '13 at 7:28
    
@PersistentNewbie its just injection of scope. You can remove it –  Maxim Shoustin Dec 14 '13 at 8:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.