javascript 对象不是 AngularJS 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19322853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object not a function in AngularJS
提问by JohnRobertPett
I have a controller that seems to be misbehaving. I have removed all the other code that works to make this short:
我有一个似乎行为不端的控制器。我已经删除了所有其他可以使这个简短的代码:
Controller:
控制器:
'use strict';
angular.module('AppliedSiteApp').controller('CarouselCtrl', function ($scope) {
$scope.nextImage = function() {
console.log('hi');
}
});
View:
看法:
<div class="carousel" ng-controller="CarouselCtrl">
<ul class="nav">
<li ng-click="prevImage()"><</li>
<li ng-click="nextImage()">></li>
</ul>
</div>
Every time I click the button in the browser it says: 'TypeError: object is not a function' or 'no method replace'. What am I doing wrong?
每次我单击浏览器中的按钮时,它都会显示:“TypeError: object is not a function”或“no method replace”。我究竟做错了什么?
回答by Olu
Are you still having a problem with this?
你还在为这个问题烦恼吗?
I ran into the same issue. The problem for me was that the function name in the controller and view was the same name as a form I was using in the same view.
我遇到了同样的问题。我的问题是控制器和视图中的函数名称与我在同一视图中使用的表单名称相同。
Changing either the form name or the function name fixed the error for me.
更改表单名称或函数名称为我修复了错误。
回答by jbarnett
Something is wrong with the way you are wiring things I believe. I usually use this scaffold:
我相信你接线的方式有问题。我通常使用这个脚手架:
angular.module('AppliedSiteApp.controllers', []).
controller('CarouselCtrl', ['$scope', function($scope) {
$scope.nextImage = function() {
console.log('hi');
}
}]);
The first argument to controller
is the name, and the second is an array. In the array, you define what services you are injecting into your controller, then you define the callback function with the injected services as parameters.
第一个参数controller
是名称,第二个参数是一个数组。在数组中,您定义要注入控制器的服务,然后使用注入的服务作为参数定义回调函数。
回答by Martin
When creating a new module you need to specify a second parameter (its dependencies). If you have none, simply pass an empty array.
创建新模块时,您需要指定第二个参数(其依赖项)。如果没有,只需传递一个空数组。
angular.module('AppliedSiteApp', [])...
Your example could access an existingmodule (that's when you don't specify the second argument), but then there's probably some code missing to spot the error (or I'm just blind).
您的示例可以访问现有模块(即您未指定第二个参数时),但是可能缺少一些代码来发现错误(或者我只是瞎了眼)。
回答by Sangram Singh
try the following definition of controller
试试下面的控制器定义
var AppliedSiteApp = angular.module('AppliedSiteApp', []);
function CarouselCtrl($scope) {
$scope.nextImage = function() {
console.log('hi');
}
}
回答by reblace
I agree with the other responses that it's an issue with your wiring. Try this fiddle as an example: http://jsfiddle.net/reblace/7fVQR/
我同意其他回答,即您的接线有问题。试试这个小提琴作为例子:http: //jsfiddle.net/reblac/7fVQR/
Declare your outer div like this:
像这样声明你的外部 div:
<div ng-app="app" ng-controller="MainController">
And then your controller like this:
然后你的控制器是这样的:
var app = angular.module('app', []);
function MainController($scope) { ... }