0

I am new to AngularJS.

I am following MEAN Stack Intro: Build an end-to-end application

I have simple html file to test .

index.html

<html>

    <head>
        <meta charset="utf-8">
        <title>Stack POC</title>
        <script type="application/javascript" src="app/scripts/controllers/user_controller.js"></script> 
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>
    <body ng-app="userListApp">
        <div ng-controller="userListController">
            <h1>No of users {{userCounts}}</h1>
        </div>
    </body>
</html>

app/scripts/controllers/user_controller.js

angular.module('userListApp', []).controller('userListController', function($scope){ 
                $scope.userCounts = 10;
                });

But when I try to run this, its not giving.

No of users {{ userCounts }}

Instead of 10 in count.

I am using and to run dev server.

gulpfile.js

var gulp = require('gulp'),
    connect = require('gulp-connect');

gulp.task('serve', function() {
    connect.server({
        root: '.',
        port: 8888
    }); 
}); 

gulp.task('default', ['serve']);

package.json

{
  "name": "poc",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-connect": "^3.2.2"
  }
}

Using 45.0.2 to view this page.

2
  • Could you provide a jsFiddle with your controller logic? Commented Apr 20, 2016 at 17:35
  • updated question with package file Commented Apr 20, 2016 at 17:39

2 Answers 2

1

That's because you are just providing a single function and don't provide a full controller.

Try something like this:

<html>
    <head>
        <meta charset="utf-8">
        <title>Stack POC</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> 
        <script>
        angular.module('userListApp', []).controller('userListController', function($scope){
            $scope.userCounts = 10;
        });
        </script>
    </head>
    <body ng-app="userListApp">
        <div ng-controller="userListController">
            <h1>No of users {{ userCounts }}</h1>
        </div>
    </body>
</html>

You need a module (I named it userListApp in your example) which is created by the angular.module() function. The second parameter would be the dependencies (empty array means none). On that module you can build up a controller. You have to pass $scope as an argument to the controller function to use it in your example. In the controller you could assign the referred variable like you tried it in the previous function.

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

5 Comments

If i move script to js file and include that js in head section, then it stop working :(. Do you have any idea why it is like so ?
It should still work. The only important thing is to add the new script reference below the angular import. Is there an error message in the console or something?
I updated my question, there is not error in console
There is no reference to angular anymore (in your updated html file). Insert your angular script tag above the other script tag: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
And I don't mean the console which you use for your gulp build, I mean the web console. It's depending on your bowser but in most cases you can open the console by pressing the f12 key.
1

You are not instantiating AngularJS app anywhere in your script. You need to provide the application name as an argument to ng-app in body tag and modify the script as well.

The function userListController needs to be attached to AngularJS controller using app.controller.

Please refer the following working fiddle:

https://jsfiddle.net/akonchady/7kfv4ssk/1/

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.