1

I have the following html:

<div ng-repeat="string in myStrings">
  <p>{{string}}</p>
</div>

And a string like this that gets added to $scope.myStrings:

$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: {{2+2}}';

I would like the string to show 4 instead of the {{2+2}} angular expression.

Am I barking up the wrong tree here by trying to do this via $compile? If not, how is it done? Just putting it in compile fails. Do I absolutely HAVE to do this in a directive?

PLNKR FOR REFERENCE

5
  • I need to put ng-click that prompts a page refresh in that string. The {{2+2}} stuff was just an example...yea, sounds like I need a directive. Commented Mar 17, 2016 at 18:16
  • The reason it isn't working is because you are putting the {{...}} inside a string, which is a variable. This is taken as a string, and not interpolated as an angular directive in the HTML. Commented Mar 17, 2016 at 18:18
  • Check this link out, it may give you the answer you are looking for: stackoverflow.com/questions/27291864/… Commented Mar 17, 2016 at 18:19
  • Thanks, looking at it now. Commented Mar 17, 2016 at 18:21
  • Can you not do? $scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + (2+2); Or are you wanting to count the actual number of strings? Commented Mar 17, 2016 at 18:21

4 Answers 4

1

Not sure what your exact goal is, but I can think of two approaches to accomplish this without compiling:

1) Split up the values like so:

<div ng-repeat="string in myStrings">
  <p>{{string}}{{mathValue}}</p>
</div>

in controller:

$scope.mathValue = 2+2;

2) Use a function to return the string (I like using this anytime I'm doing anything binding that is non-trivial):

<div ng-repeat="string in myStrings">
  <p>{{stringFunction()}}</p>
</div>

in controller:

$scope.mathValue = 2+2;
$scope.stringFunction = function() {
    return 'I want to count to 4 via angular: '+$scope.mathValue;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted since looks like I confused people. {{2+2}} wasjust an example.I need compiled html where that string would normally go.
1

I'm not 100% sure whether you are just wanting to count the number of strings in the myStrings array, or just have the ability to add a count, but given your Plunker, you could do the following:

To simply add two variables, update the following line:

$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + (2+2);

If you wanted to show the count of the number of strings, swap the order of your scope variable declarations and show the myStrings length

 $scope.myStrings = ['I am a string', 'I am another string', 'more strings!'];

 $scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' + $scope.myStrings.length;

Counting the strings will only give you 3, of course, because there are only 3 strings in the array.

Does that solve it for you?

UPDATE

OK - So I think what you want is the count in the string with an ng-click to correspond to the count correct?

If so, then the following on your ng-repeat would do it...

<p><a href="" ng-click="someMethod($index)">{{string}} {{$index}}</a>   </p>

Using $index gives you the index of the repeating item. You can always add 1 to the $index to make it 1-based instead of zero based:

<p><a href="" ng-click="someMethod($index)">{{string}} {{$index + 1}}</a>   </p>

2 Comments

Hey, I am re-posting my comment: "I need to put ng-click that prompts a page refresh in that string. The {{2+2}} stuff was just an example...yea, sounds like I need a directive". I appreciate the reply, but I am looking for a way to compile the string into html via angular. Looks like I am doing something sill to begin with.
OK - tweaked my answer slightly based upon your comment.
0

You can append the angular expresion {{}} to the string like:

$scope.stringIwantToBeCompiled = 'I want to count to 4 via angular: ' +  {{stuff or 2 + 2}};

Or use $compile Fiddle example

Comments

0

I really needed to use a directive with $compile like shown here:

app.directive('dynamicAlert', function ($compile) {
  return {
    restrict: 'A',
    replace: true,
    link: function (scope, ele, attrs) {
      scope.$watch(attrs.dynamicAlert, function(html) {
        ele.html(html);
        $compile(ele.contents())(scope);
      });
    }
  };
});

http://plnkr.co/edit/bJPEyfkKsertulTN7rWp?p=preview

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.