AngularJS无法正常工作。无法调试错误

时间:2016-09-05 06:07:37

标签: javascript angularjs

MY HTML PAGE

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="assets/js/angular.min.js"></script>
    <script src="assets/js/app.js"></script>
</head>
<body>
    <div ng-controller="HelloController">
        <h2>Hi {{helloTo.title}}, Start learning angular js</h2>

        Enter Your Name<input type="text" ng-model="name">
        <span ng-bind="name"></span>
    </div>
    <div ng-controller="studentController">

    </div>

</body>

和我的JS

function HelloController($scope) {
$scope.helloTo = {};
$scope.helloTo.title = "Rahul Devan";
}

我需要在同一个JS文件中的同一页面中使用多个控制器。这就是我使用这种控制器的原因。

我得到这样的输出 Output I am getting

请帮助找出错误

1 个答案:

答案 0 :(得分:0)

嘿,请尝试此示例,了解如何为同一个应用模块使用多个控制器。

var app = angular.module('sample', []);


app.controller('first', function($scope) {
  $scope.name = 'James';

});


app.controller('second', function($scope) {

  $scope.name = 'John';
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="first">First Controller:
    <span>{{name}}</span>
  </div>
  <div ng-controller="second">Second Controller:
    <span>{{name}}</span>
  </div>

</body>