简体   繁体   English

AngularJS ng-click不起作用

[英]AngularJS ng-click not working

I have just created a very simple app. 我刚刚创建了一个非常简单的应用程序。 It only contains one function which is used in ng-click. 它仅包含ng-click中使用的一个功能。 Somehow, ng-click is not firing. 不知何故,ng-click不会触发。 Can anyone take a look for me? 谁能帮我看看?
Plunker: 柱塞:
http://plnkr.co/edit/DxwX5sJVaKABeC2JBF8M?p=preview http://plnkr.co/edit/DxwX5sJVaKABeC2JBF8M?p=preview

HTML 的HTML

var app = angular.module('myApp');

app.controller('createController', ['$scope', function($scope){
    $scope.submitProject = function(){
    alert('wahaha');
};

}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
</head>
<body>
    <h1>Hello Plunker!</h1>
    <div class="form-group text-right">
        <button class="btn btn-primary btn-lg" ng-click="submitProject()">Submit</button>
    </div>
</body>
</html>

you need to bind your controller by adding an ng-controller attribute like 您需要通过添加ng-controller属性来绑定控制器,例如

<body ng-controller="createController"> 

to an element that encompasses the html that you would like to bind the scope of your createController to. 到包含要绑定createController范围的html的元素。

Add an ng-controller, so ng-click will know which controller is the function. 添加一个ng-controller,这样ng-click就会知道哪个控制器是该功能。

    <div class="form-group text-right" ng-controller="createController as create">
        <button class="btn btn-primary btn-lg" ng-click="create.submitProject()">Submit</button>
    </div>

I recommend using controllerAs, try reading about this in this article . 我建议使用controllerAs,试试这个在阅读这个文章

Adding the controller to an outer Div will solve your problem. 将控制器添加到外部Div可以解决您的问题。

 <div ng-controller='createController' class="form-group text-right">

Hope it helps. 希望能帮助到你。

It looks like the module 'myApp' is not correctly instantiated. 看起来模块'myApp'没有正确实例化。 It should be: 它应该是:

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

This is because you need to pass in an array which is the list of modules myApp depends on. 这是因为您需要传递一个数组,该数组是myApp所依赖的模块列表。 In this case, it is an empty array. 在这种情况下,它是一个空数组。

You will also need to add the controller to the view as suggested by previous answers. 您还需要按照先前答案的建议将控制器添加到视图中。

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

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