0

The button code is

<div class="btn-group" align="center">
    <button ng-click="myFunction()" id="one" class='btn btn-primary btn1' >Save Entry</button>
    <button ng-click="close_window()" id="two" class='btn btn-primary btn2'>Exit</button>
</div>

The New controller is

var app=angular.module('userApp',[])
    app.ng-controller('userController', function($scope)){
        $scope.myFunction = function(){
            var change= document.getElementById("one")
            if (change.innerHTML == "Save Entry")
            {
                change.innerHTML = "Saved!";
            }
            else 
            {
                change.innerHTML = "Save Entry";
            }
            return
        }

        $scope.close_window = function() {
            if (confirm("Close Window?")) {
                window.close();
            }      
        }
    }

this below code was working fine if i didn't add any controller and just declared function's like

function myFunction(){
    var change= document.getElementById("one")
    if (change.innerHTML == "Save Entry")
    {
        change.innerHTML = "Saved!";
    }
    else 
    {
        change.innerHTML = "Save Entry";
    }
}

function close_window() {
    if (confirm("Close Window?")) {
    close();
}

but when i add the controller the buttons would stop working. I tried every function definitions and answers on stack overflow but nothing seem to be working correctly. How do i get to button's work inside controller like they used to be working?

3
  • 1
    Change this app.ng-controller to app.controller, which is the correct syntax for making a controller. Commented Aug 23, 2017 at 11:37
  • a semicolon is missing var app=angular.module('userApp',[]). also you need to change the controller code like below app.controller Commented Aug 23, 2017 at 11:41
  • corrected those still not working. Commented Aug 23, 2017 at 11:48

1 Answer 1

2

var app=angular.module('userApp',[])
    app.controller('userController', function($scope){
        $scope.myFunction = function(){
            var change= document.getElementById("one")
            if (change.innerHTML == "Save Entry")
            {
                change.innerHTML = "Saved!";
            }
            else 
            {
                change.innerHTML = "Save Entry";
            }
            return
        }

        $scope.close_window = function() {
            if (confirm("Close Window?")) {
                window.close();
            }      
        }
    });
<DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="userApp" ng-controller="userController">
<div class="btn-group" align="center">
    <button ng-click="myFunction()" id="one" class='btn btn-primary btn1' >Save Entry</button>
    <button ng-click="close_window()" id="two" class='btn btn-primary btn2'>Exit</button>
</div>
</body>

you are using app.ng-controller it should be app.controller.ng-controller is a directive, used to define controller in view.

app.controller('userController', function($scope){
    .......
})
Sign up to request clarification or add additional context in comments.

5 Comments

have you specified controller for the view in routing or html file? Like ng-controller = "userController"
Yes i did like this <body ng-app="userApp" ng-controller="userController">
No the page is working fine but the functions are not doing their specified work.
i have updated my answer with working snippet, please check, hope it would help!!
thanks @the_mishra now it's working fine...!! Thanks a ton!!

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.