O
O
Oriaraniar2016-01-08 15:53:07
Angular
Oriaraniar, 2016-01-08 15:53:07

Can't connect Angular controller. Where am I wrong?

Good afternoon. In the lessons I'm trying to learn how to use angular.
Project on ASP.NET MVC
Created a simple page

<!DOCTYPE html>
<html ng-app="appName">

<head>

    <script src="~/main.js"></script>
</head>

<body ng-controller="mainController">
    <h1>Angular Playground</h1>
    {{message}}
    <br />Total Length: {{message.length}}
</body>

</html>

js file created
function mainController($scope) {
        $scope.message = "Hello, Michael";
    }

    angular
        .module("appName", [])
        .controller("mainController", mainController);

As a result, I get the error
Error: [ng:areq] Argument 'mainController' is not a function, got undefined
Angular version v1.4.8.
Studio 2013.
Chrome browser.
Where am I wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oriaraniar, 2016-01-10
@Oriaraniar

If you create a completely empty page with this code - it works.
If you run through the studio - it does not work.

<!DOCTYPE html>
<html ng-app="appName">

<head>
</head>

<body ng-controller="mainController">
<h1>Angular Playground</h1>
{{message}}
<br/>Total Length: {{message.length}}
</body>

<script>
    function mainController($scope) {
        $scope.message = "Hello, Michael";
    }

    angular
        .module("appName", [])
        .controller("mainController", mainController);
</script> 
</html>

I suspect that some studio settings are not set correctly, but I can’t figure out what exactly.
UPD
, the problem was that there was an incorrect declaration of angular in the upstream layout.
When changing this line, <html xmlns:ng="http://angularjs.org" >everything worked

M
Mikhail Osher, 2016-01-08
@miraage

You are doing something wrong.
DEMO .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question