简体   繁体   English

ng-单击包含ng的按钮

[英]ng-click on button that is ng-included

I have a problem. 我有个问题。 I'm trying to include a button that's on a html template page on to my index.html page. 我试图在index.html页面的html模板页面上包含一个按钮。 I do it like this 我这样做

<ng-include src="'logout/logout.template.html'"></ng-include>

The page is: 该页面是:

<button type="button" class="btn btn-default btn-sm" ng-click="$ctrl.lala()">
    <span class="glyphicon glyphicon-log-out" ></span> Log out
</button>

The problem is, the thing doesn't work. 问题是,那东西不起作用。 It won't access the ctrl function. 它不会访问ctrl函数。 As I read, ng-include doesn't work well with other angular directives so my question is, how else can I include my button template in the index.html without copy pasting the code because it's connected to a componenet an dservice so I can't break it. 如我所读,ng-include不能与其他angular指令一起很好地工作,所以我的问题是,如何将我的按钮模板包括在index.html中而不复制粘贴代码,因为它已连接到comservice和dservice,所以我可以不要破坏它。

The componenent: 组件:

'use strict';
angular
    .module('logout')
    .component('logout', {
        templateUrl: 'logout/logout.template.html',
        controller: ['$scope', '$location', '$localStorage', 'Logout',
            function LogoutController($scope, $location, $localStorage, Logout) {


                this.lala = function () {
                    console.log("doing logout");
                }
            }

        ]
    });

I know it sounds like an overkill, but I would just create another component for it. 我知道这听起来像是一个过大的杀伤力,但我只是为此创建了另一个组件。 If lala() is the function that actually logs the user out then it's best to include that in the component too, so you don't have to repeat it in every parent component. 如果lala()是实际上将用户注销的函数,那么最好也将其包含在组件中,因此您不必在每个父组件中都重复该函数。

Take a look at this jsfiddle for a complete sample. 看看这个jsfiddle以获得完整的示例。 I wrote everything in the html, but I copied the important part below: https://jsfiddle.net/6cjd5ggq/1/ 我用html编写了所有内容,但是我复制了下面的重要部分: https : //jsfiddle.net/6cjd5ggq/1/

<logout></logout>

angular.module('logoutapp')
   .controller('logoutController', logoutController)
   .component('logout', {
      // you can use templateUrl, but it's easier this way in jsfiddle
      template: `<button type="button" class="btn btn-default btn-sm" ng-click="$ctrl.lala()">
                    <span class="glyphicon glyphicon-log-out"></span> Log out
                 </button>`,
      controller: 'logoutController'
});

function logoutController() {
  this.lala = function() {
    alert("logout!");
  };
}

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

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