jhony3 jhony3 - 4 months ago 10
AngularJS Question

AngularJS doesn't work Visual Studio?

I have just started with learning AngularJS and everthing works great before I started to write some easy controller method and method doesn't work even i include ng-contoller I tried to include it as separated js file but that also doens't work I am new to angularJS so I would appreciate any help I also tried with notped++ to do the same but doesn't work this is my code;

<!DOCTYPE html>
<html data-ng-app="">
<head>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/angular.min.js"></script>

</head>
<body>
<div class="container" data-ng-controller="AppController">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav-toggle">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Contact Manager</a>

</div>
<div class="collapse navbar-collapse" id="nav-toggle">
<ul class="nav navbar-nav">
<li class="alert-success"><a href="/">Browse</a></li>
<li><a href="/add">Add contacts</a></li>
</ul>
<form class="navbar-form navbar-right" role="search">
<input type="text" class="form-control" placeholder="Search"/>
</form>
</div>
</nav>
<div class="page-header">
<h2>Prvo poglavlje <small>Hello world</small></h2>
</div>
<div class="container">
<div class="jumbotron">
<h1>Hello, {{name||"World"}}</h1>
<input type="text" class="form-control input-lg" data-ng-model="name" />

</div>
<div class="row">
<div class="col-sm-8 pull-right hidden-lg" >
<div class="row">
<div class="col-sm-6">
<p>Prvi</p>
</div>
<div class="col-sm-6">
<p>Drugi</p>
</div>
</div>
</div>
<div class="col-sm-4 pull-left show" >
This is our sidebar
</div>
</div>
<div class="row">
<div class="col-md-8">
<button ng-click="clickHandler()">Click me</button>
</div>
</div>
</div>
</div>

<script>
function AppController($scope) {
$scope.clickHandler = function () {
window.alert("Clicked");
};
}
</script>
</body>
</html>

Answer

You have to define a module before instantiate your controller:

angular
  .module('app', [])
  .controller('AppController', AppController);

AppController.$inject = ['$scope'];

function AppController($scope) {
  $scope.clickHandler = function () {
    window.alert("Clicked");
  };
}
<!DOCTYPE html>
<html ng-app="app">

<head>
  <title></title>
  <meta charset="utf-8" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body>
  <div class="container" ng-controller="AppController">
    <nav class="navbar navbar-default" role="navigation">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav-toggle">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/">Contact Manager</a>

      </div>
      <div class="collapse navbar-collapse" id="nav-toggle">
        <ul class="nav navbar-nav">
          <li class="alert-success"><a href="/">Browse</a></li>
          <li><a href="/add">Add contacts</a></li>
        </ul>
        <form class="navbar-form navbar-right" role="search">
          <input type="text" class="form-control" placeholder="Search" />
        </form>
      </div>
    </nav>
    <div class="page-header">
      <h2>Prvo poglavlje <small>Hello world</small></h2>
    </div>
    <div class="container">
      <div class="jumbotron">
        <h1>Hello, {{name||"World"}}</h1>
        <input type="text" class="form-control input-lg" data-ng-model="name" />

      </div>
      <div class="row">
        <div class="col-sm-8 pull-right hidden-lg">
          <div class="row">
            <div class="col-sm-6">
              <p>Prvi</p>
            </div>
            <div class="col-sm-6">
              <p>Drugi</p>
            </div>
          </div>
        </div>
        <div class="col-sm-4 pull-left show">
          This is our sidebar
        </div>
      </div>
      <div class="row">
        <div class="col-md-8">
          <button ng-click="clickHandler()">Click me</button>
        </div>
      </div>
    </div>
  </div>
</body>

</html>