简体   繁体   English

AngularJS:ng-click不起作用

[英]AngularJS: ng-click not working

I have a group-button with is included in a template add-new-document-template.html : 我在模板add-new-document-template.html包含一个组按钮:

<div class="col-sm-4" style="text-align: right">
        <div class="btn-group" role="group">
            <button class="btn btn-default" ng-class="{active: layout == 'list'}"
                    ng-click="listView()">
                <i class="fa fa-list-ul fa-lg"></i></button>
            <button class="btn btn-default" ng-class="{active: layout == 'grid'}" ng-click="gridView()">
                <i class="fa fa-th fa-lg"></i></button>
        </div>
    </div>

Here is the directive: it's inculeded in an a parent directive 这是指令:它是在父指令中灌输的

angular.module('bdocinteractive.addNewDocument')
.constant("NEW_DOC_TEMP_URL", {
    url: 'src/app/add-new-document/add-new-document-template.html'
})
.directive("addNewDocument", ["NEW_DOC_TEMP_URL", function (tempURL) {
    return {
        scope: true,
        restrict: 'E',
        controller: 'AddNewDocumentController',
        controllerAs: "addDocCtrl",
        templateUrl: tempURL.url
    }
}]);

And here is my Controller: 这是我的控制器:

angular.module("bdocinteractive.addNewDocument").
controller("AddNewDocumentController", ["$scope", "AddNewDocumentService",
    function ($scope, AddNewDocumentService) {
        var me = this;
        $scope.gridView = function () {
            $scope.layout = "grid";
        };
        $scope.listView = function () {
            $scope.layout = "list";
        };
        $scope.models = [];
        $scope.layout = "list";
        $scope.modelSelectedId = "";
        AddNewDocumentService.getAllModels().success(function (data) {
            $scope.models = data;
        });
    }]
);

When clicking on the button, the ng-click is not fired 单击按钮时,不会触发ng-click

Can you help me please 你能帮我吗

NB: I'am working with Angular 1.3.15 Version 注意:我正在使用Angular 1.3.15版本

Do you have an alert method defined on your $scope ? 您在$scope上定义了一个alert方法吗? Expressions in ng-click are not evaled on the global object (window), so you can't reference any global variables in that expression. ng-click中的表达式不会在全局对象(窗口)上规避,因此您不能在该表达式中引用任何全局变量。 You need something like: 您需要类似:

$scope.alert = window.alert;

This is an updated answer... 这是更新的答案...

I assume you are having a problem with toggling the value of $scope.layout the above example seems to be working. 我假设您在切换$scope.layout的值时$scope.layout ,上面的示例似乎可以正常工作。

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

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