1

I have a very basic example that isn't working: I have two aspects, the index.html and the main.js. They are in the same folder:

<!DOCTYPE html>
<html>
<head>
        <title> AngularJS Tutorials </title>
        <link rel="stylesheet" href="css/foundation.min.css">
        <script src="main.js"></script>
</head>
<body>

<div ng-app="myApp">
    <div ng-controller="FirstCtrl">
        <h1>{{data.message}}</h1>
    </div>
</div>

<script type="text/javascript" src="js/angular.min.js"></script>
</body>
</html>

main.js

function FirstCtrl($scope) {
    $scope.data = {message: "Hello"};
}

my page shows this:

{{data.message}}

2 Answers 2

2

You need to add the controller to the angular module. you can use the controller function to add the js function for your controller.

var FirstCtrl = function FirstCtrl($scope) {
    $scope.data = {message: "Hello2"};
};

angular.module("myApp",[]).controller("FirstCtrl",FirstCtrl);

If you already had the angular module defined somewhere else in the page, Use this version.

angular.module("myApp").controller("FirstCtrl",FirstCtrl);

Here is a working sample http://jsbin.com/tiroteharu/edit?html,js,console,output

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

Comments

2
<script src="~/Scripts/angular/angular.js"></script>
<script>
    var myApp = angular.module('myApp', []);

    myApp.controller('FirstCtrl', ['$scope', function ($scope) {
        $scope.data = { message: "Hello" };
    }]);

</script>



<div ng-app="myApp">
    <div ng-controller="FirstCtrl">
        <h1>{{data.message}}</h1>
    </div>
</div>

More help read: https://docs.angularjs.org/guide/controller

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.