0

I'll appreciate any help. My problem: I can't show value from $scope.info in my view. I don't have direct access to html, so I can change DOM only via js. I created new element and added it to the DOM with append() method

$scope.info = {
  clicks: 15862,
  spent: '$124',
  created: '04/08/2017',
  author: 'John Kovalenko',
  name: 'Advertise US'
};

var info = document.createElement('div');
info = angular.element(info).attr({
  'ng-bind': 'info',
  'class': 'statistic-info'
});
angular.element(infoContainer).append(info[0]);

I created example here. http://jsfiddle.net/ADukg/13598/

What I'm doing wrong? Thank you

2 Answers 2

4

You have to use $compile when you manually inject anything that uses directives

  var info = angular.element('<div>').attr({
    'ng-bind': 'info.author',
    'class': 'statistic-info'
  });

  info = $compile(info)($scope);

  angular.element(infoContainer).append(info);

Be sure to inject $compile wherever you use it

DEMO

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

Comments

2

The simplest answer is to add the data into the HTML content before appending it. Since you have an object, I've supplied one of it's properties in the snippet below.

What is happening is your page loads, and the digest cycle has already completed. What you are appending is simply plain text, and therefor is not interpolated. This method that I am providing will not provide two-way binding. If that is required, you should be using a directive.

var infoTwo = '<div><p>' + $scope.info.clicks + '</p></div>';

That said, charlieftl has the best answer.

Comments

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.