Angular JS控制器初始化错误

时间:2016-05-16 10:18:11

标签: javascript angularjs

我刚开始使用Angular.js控制器,当我尝试初始化角度控制器时,我收到错误Uncaught TypeError: app.controlller is not a function

看看我的代码段。



var app = app;
if (!app) {
  app = angular.module('app', []);
  console.log("created app");
};
app.controlller('languages', ['$scope',
  function($scope) {
    $scope.selected = 'None';
    $scope.js = function() {
      $scope.selected = 'JavaScript';
    };
    $scope.cpp = function() {
      $scope.cpp = 'C++';
    };
  }
]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='app'>
  <div ng-controller='languages'>
    Select a language:
    <button ng-click='js()'>JavaScript</button>
    <button ng-click='cpp()'>C++</button>
    <p>You have selected {{ selected }}</p>
  </div>
</body>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

控制器中存在TYPO错误

app.controlller这应该是app.controller

答案 1 :(得分:1)

修正了拼写错误controlller并更改了cpp()功能以设置$scope.selected = 'C++'

&#13;
&#13;
var app = app;
if (!app) {
  app = angular.module('app', []);
  console.log("created app");
};
app.controller('languages', ['$scope',
  function($scope) {
    $scope.selected = 'None';
    $scope.js = function() {
      $scope.selected = 'JavaScript';
    };
    $scope.cpp = function() {
      $scope.selected= 'C++';
    };
  }
]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='app'>
  <div ng-controller='languages'>
    Select a language:
    <button ng-click='js()'>JavaScript</button>
    <button ng-click='cpp()'>C++</button>
    <p>You have selected {{ selected }}</p>
  </div>
</body>
&#13;
&#13;
&#13;