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";
}
Pravee
  • 85
  • 1
  • 10

3 Answers3

3

You can use ng-if, or $compile

Community
  • 1
  • 1
terafor
  • 1,620
  • 21
  • 28
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>
Nattu Raju
  • 310
  • 1
  • 2
  • 11
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.

Duncan
  • 92,073
  • 11
  • 122
  • 156