javascript - controller's function won't work in angular js -
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 working fine if didn't add controller , declared function's
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 add controller buttons stop working. tried every function definitions , answers on stack overflow nothing seem working correctly. how button's work inside controller used working?
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 using app.ng-controller
should app.controller
.ng-controller
directive, used define controller in view.
app.controller('usercontroller', function($scope){ ....... })
wiki
Comments
Post a Comment