1

I have created a controller module in a javascript file,

myController.js

(function () {
    "use strict";
    var app = angular.module("mymodule");

    app.controller("MyController", ["$scope", "service", 
       function ($scope, service) {
            $scope.doSomething = function(){
                var element = service.getElement(1);
                element.events.register("mouseover", element, function(){});
            }
       }
    ]);
)();

I referenced in in the index.html file like following.

<script src="js/lib/myController.js"></script>

And I used state to call my controller template.

app.config(["$stateProvider", function ($stateProvider) {
    $stateProvider.state("myctrl", {
        url: "/myctrl",
        templateUrl: "templates/myctrl/myctrl.html",
        controller: "MyController"
    });
}])

I am putting a breakpoint to $scope.doSomething function. When I started the application the breakpoint stopped that point. And when I call url of my controller template url, breakpoint is firing again. So my event registered twice, and event firing two times.

I think controller is creating two times or another problem.

Solution:

4
  • 1
    What is your actual HTML? Commented Sep 12, 2014 at 6:44
  • 1
    oohh OK I looked my HTML and figurout the problem. My href link contains ng-controller and template contains controller. So two times called. Commented Sep 12, 2014 at 6:50
  • 1
    You should post this as an answer and accept it so others can see that your problem was solved, especially since you solved it yourself. Don't add the answer to the question itself. Add your HTML to the question instead. Commented Sep 12, 2014 at 6:50
  • listen to $destroy if you have custom listeners... am not sure with angular-ui, but with route, controllers gets instantiated as often as the url changes. Commented Sep 12, 2014 at 6:55

2 Answers 2

1

I looked my HTML and figurout the problem. My href link that called template url contains ng-controller="MyController" and template contains controller. So two times called.

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

Comments

0

From the Angular Docs:

Note that you can also attach controllers to the DOM by declaring it in a route definition via the $route service. A common mistake is to declare the controller again using ng-controller in the template itself. This will cause the controller to be attached and executed twice.

Comments

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.