0

HTML

<div class="container" ng-controller="main" style="margin-top: 30px;">
    <div class="row">
        <div class="col-md-12">
            <p>{{firstName}}</p>
            <br>
            <p>{{lastName}}</p>
            <br>
            <button class="btn btn-primary" ng-click="showLog()">MyButton</button>
        </div>
    </div>
</div>


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

main.js

angular.module('app').controller('main',
function ($scope, $http, $state, $sce, urls) {

$scope.firstName = 'John';
$scope.lastName = 'Doe';

$scope.showLog = function () {
    console.log("angular works");
}

});

I'm fairly new to AngularJS and am just trying to connect Angular File with Html. so far I created the showLog function but pressing it does nothing at all. I feel like ng-controller does nothing and {{firstName}} & {{lastName}} are showing just like that (meaning the 'John' 'Doe' don't get injected)

Also I have another question - If I'm loading the Angular file with <script type="text/javascript" src="js/main.js"></script> , Why do I need to specify ng-controller ? shouldn't this already work?

Console

1
  • If you click on the link in the error message, it goes to a webpage that explains the error. Unknown provider: $stateProvider <- $state <- main. Providing text of the error message is more helpful than a picture. This error results from the $injector being unable to resolve a required dependency. Commented Mar 2, 2020 at 20:39

2 Answers 2

1

app module is not registered correctly. You need to instantiate like this:

angular.module('app', [])
angular.module('app', []).controller('main',
function ($scope) {

$scope.firstName = 'John';
$scope.lastName = 'Doe';

$scope.showLog = function () {
    console.log("angular works");
}

});

In order to start the application , you need to use ng-app directive (https://docs.angularjs.org/api/ng/directive/ngApp):

<div class="container" ng-app="app" ng-controller="main" style="margin-top: 30px;">
    <div class="row">
        <div class="col-md-12">
            <p>{{firstName}}</p>
            <br>
            <p>{{lastName}}</p>
            <br>
            <button class="btn btn-primary" ng-click="showLog()">MyButton</button>
        </div>
    </div>
</div>

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

6 Comments

ng-app directive doesn't show in directives list when I type it, and also if I add the directive console then shows : angular.min.js:7 Uncaught Error: [$injector:modulerr]
ng-app is required to be placed into the html of your app. Without that, the controller will not be instantiated. That is the console error?
I edited my question and added the image of the console
It works, the only difference between our codes is that I have several unnecessary injections in my function() , removing everything except $scope & $http fixed the problem. Thank you very much good sir
It was the next thing I would suggest you to try to remove these injections
|
0

You can use angular.bootstrap for connecting the app to specific html element.

angular.module('app', [])
  .controller('main', ['$scope', function exampleController($scope) {
    $scope.firstName = 'John';
    $scope.lastName = 'Doe';

    $scope.showLog = function() {
      console.log("angular works");
    }
  }]);

angular.bootstrap(document.body, ['app']);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="container" ng-controller="main" style="margin-top: 30px;">
  <div class="row">
    <div class="col-md-12">
      <p>{{firstName}}</p>
      <br>
      <p>{{lastName}}</p>
      <br>
      <button class="btn btn-primary" ng-click="showLog()">MyButton</button>
    </div>
  </div>
</div>

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.