2

I need to generate 'div' dynamically without the help of ng-repeat and also different data in the scope.

<div attribute={{atrb}}>{{name}}</div> (<- dynamic div).

Controller

$scope.GenerateDiv = function()
{
$scope.name = "praveen";
$scope.atrb = "01";
}
2
  • 1
    What is the reason you don't want to use ng-repeat when it's clearly the best and simplest solution ? Commented Feb 29, 2016 at 12:38
  • 1
    you can create a directive, where you can pass data, and a template. In the template you can put the html Commented Feb 29, 2016 at 12:49

3 Answers 3

3

You can use ng-if, or $compile

Sign up to request clarification or add additional context in comments.

Comments

2
You can do it like

$scope.str = ''
for ( i = 0 ; i < arrayofdata ; i++ ){
  $scope.str += "<div>"+arrayofdata[0].ex+"</div>"
}


use Sanitize filter the following link contains log
https://docs.angularjs.org/api/ngSanitize

then create html like this
<div>
{{str | Sanitize}}
</div>

Comments

1

If it is a single div you use ng-if to control the creation of it:

<div attribute="{{atrb}}" ng-if="name">{{name}}</div>

When the condition is not met the div is not created. When the condition becomes true, the div will be added to your DOM.

If you need several of them then store the parameters in an array and use ng-repeat.

2 Comments

it is not single,i need to generate multiple div
that's what ng-repeat is for.

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.