简体   繁体   English

Angular JS控制器初始化错误

[英]Angular JS Controller Initialization Error

I'm just starting out with Angular.js controller and I'm getting an error Uncaught TypeError: app.controlller is not a function when I try to initialize the angular controller. 我刚开始使用Angular.js控制器,但遇到错误Uncaught TypeError: app.controlller is not a function尝试初始化角度控制器时, Uncaught TypeError: app.controlller is not a function

Have a look at my snippet. 看看我的片段。

 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> 

There is TYPO error in controller 控制器中有TYPO错误

app.controlller this should be app.controller app.controlller这应该是app.controller

Fixed typo controlller and changed cpp() function to set $scope.selected = 'C++' . 固定错字controlller和改变cpp()函数来设置$scope.selected = 'C++'

 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++'; }; } ]); 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM