0
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js">              
    </script>
</head>
<body style="padding: 20px 20pc;">
    <div ng-app="app">
        <div ng-repeat="item in 'somewords'.split('')">
            {{$index + 1}}. {{item}}
        </div>
    </div>
    <script type="text/javascript">
    </script>
</body>
</html>

Hi everyone, I've tried to look up for some old post already but found nothing that could really help me. I'm learning Angular and in a Tutorial they use this code that is supposed to count and split the letters in the word given. The problem I'm having is that I get the curly braces as if they where some text in HTML. What am I doing wrong?

2
  • Have you got any console errors? Commented Sep 26, 2016 at 8:37
  • Do you have a js file? You still need to initiate the app. Commented Sep 26, 2016 at 8:38

2 Answers 2

2

I see a ng-app="app", but I don't see any code that initializes a module (with the name 'app'). Because you don't use any data that is initialized in any code, you can just rewrite it as <div ng-app>.

The other possibility is to define the module (which you'll probably need anyway if you dive deeper in the tutorials):

angular.module("app", []);

EDIT

As @Peter_Fretter correctly mentioned, it will still not work, because you have duplicates in the ng-repeat. You can fix that with using the track by $index:

<div ng-repeat="item in 'somewords'.split('') track by $index">
    {{$index + 1}}. {{item}}
</div>

See this jsfiddle

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

1 Comment

It's possible he didn't post the JS file. Even with that declaration of the module, the code would not work. Check out my answer for the problem.
0

You have problems with the duplicates. You should use track by to solve the issue.

<div ng-repeat="item in 'somewords'.split('') track by $index">
        {{$index + 1}}. {{item}}
</div>

Here is a codepen. Also here is some documentation.

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.