5

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()">&lt;</li>
        <li ng-click="nextImage()">&gt;</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?

3
  • 2
    Do you have the prevImage() defined? Are you clicking the prevImage() button? Commented Oct 11, 2013 at 16:30
  • Did you ever find an answer for this? I have the case with the same output but only after the first click, which works fine. Commented Apr 3, 2014 at 16:49
  • I used a function which is reserved.. $scope.register = fun... won't work. Commented Apr 4, 2014 at 22:30

5 Answers 5

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. If I remember correctly, it was to do with a naming issue. My own fault!
3

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.

Comments

1

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 existing module (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).

2 Comments

That makes the controller get returned as undefined now. Thanks though.
Try to compare your code to this exampe: plnkr.co/edit/igQ93Kt0SFkfJMvBRekY?p=preview
0

try the following definition of controller

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

function CarouselCtrl($scope) {

    $scope.nextImage = function() {
        console.log('hi');
    }
}

5 Comments

Not too sure how you want me to implement that one. Can you give me a nudge in the right direction?
delete ur entire controller code. and type what's written above.
including 'use strict'
Ok, tried that. Console says: 'Error: Argument 'MainCtrl' is not a function, got undefined'
hav u put 'ng-app = 'AppliedSiteApp' ? along with html?
0

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/

Declare your outer div like this:

<div ng-app="app" ng-controller="MainController">

And then your controller like this:

var app = angular.module('app', []);
function MainController($scope) { ... }

2 Comments

I have tried this and it doesn't work for me. I am using the Angular generator also, for Yeoman, and that isn't how it creates modules, which seems to break everything else too.
The way I do it in this example only really works if it's standalone (eg. in JSFiddle). Your approach to linking the view partial to the scope function callback is fine. But there's something wrong with the wiring between the partial and the controller. Your original question doesn't really contain enough info to isolate the problem. You may want to start over and verify you can get a "Hello World" working with the setup you have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.