简体   繁体   English

控制器的功能在Angular JS中不起作用

[英]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 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? 我如何像以前那样在控制器内部进行按钮的工作?

 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 . 您正在使用app.ng-controller ,应该是app.controller ng-controller is a directive, used to define controller in view. ng-controller是指令,用于在视图中定义控制器。

app.controller('userController', function($scope){
    .......
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM