Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angular.js中单击html元素的触发器_Javascript_Angularjs - Fatal编程技术网

Javascript 在angular.js中单击html元素的触发器

Javascript 在angular.js中单击html元素的触发器,javascript,angularjs,Javascript,Angularjs,我现在有一个指令。当我使用click to edit指令单击元素时,将显示一个文本字段来编辑内容 我想当我点击按钮时,这种行为会继续发生。 我需要的是,当按钮被点击时,它相当于点击指令。我怎样才能做到这一点 <label>Sprint name:</label> <div click-to-edit type="inputText" ng-model="sprintName"></div> <br/> <

我现在有一个指令。当我使用click to edit指令单击元素时,将显示一个文本字段来编辑内容

我想当我点击按钮时,这种行为会继续发生。 我需要的是,当按钮被点击时,它相当于点击指令。我怎样才能做到这一点

    <label>Sprint name:</label> <div click-to-edit type="inputText" ng-model="sprintName"></div>
    <br/>
    <input type='button' value='trigger Directive' ng-click='triggerDirective()'>
   </div>

修改该指令,在该指令中为button控件添加click bind事件。因此,当调用该指令时,它会绑定按钮上的click事件。
希望这有帮助

要实现您必须做的事情,您可以使用或

示例:

1-Angularjs事件:

HTML:

指令:

指令:


没有太深入的代码,但有一件事要考虑的是使用一个角事件,得到广播在TrutGrand指令,并听取它在导演,我是新的角度,我不知道如何做到这一点。你能帮我吗?读一些关于使用角度镜的教程events@charlietfl你可以给我一个链接,例子或类似的东西,我不知道如何搜索…你能给我一个例子吗?我的想法是让它在一个ng重复。根据点击的按钮,每个按钮都应该做我想做的事情……我感谢您的时间,但这很复杂,我的想法是将每个按钮包含在一个ng重复中,当我点击按钮时,我希望相应的文本字段出现。因此,您可以利用第二种方法。不太复杂,请在ng内重复,我将更新我的答案。但是,如果您有一个ng重复,您将在哪里放置toggleDirective按钮?那个按钮应该做什么?切换所有记录?看这张图片。你对我点击的每一个按钮都很友好,然后我就可以编辑这个字段了。所以,你有一个切换按钮用于ng repeat的每一项。。像这样的东西显然不是手风琴,只是一张桌子,但方法是一样的
<div click-to-edit></div>
<button type="button" ng-click="item.toggle()">Toggle from Controller</button>
app.controller('MainCtrl', function($scope) {
    $scope.toggle = function(){
        //Emit the event
        $scope.$broadcast('app.myEvent');
    };
});
app.directive('clickToEdit', function() {
    return {
        restrict: 'A',
        template: `
            <div ng-show="editState">
                <div>
                    <input type="text"/>
                    <button type="button" ng-click="item.toggle()">Cancel</button>
                </div>
            </div>
        `,
        link: function (scope, element, attrs) {
            scope.editState = false;

            scope.toggle = function () {
                scope.editState = !scope.editState;
            }

            //Listen and handler the event
            scope.$on('app.myEvent', function(event){
                scope.toggle();
            });
        }
    }
});
<div click-to-edit item="item"></div>
<button type="button" ng-click="item.toggle()">Toggle from Controller</button>
app.controller('MainCtrl', function($scope) {
    $scope.item = {};
});
app.directive('clickToEdit', function() {
    return {
        restrict: 'A',
        scope: {
            item: '='
        }
        template: `
            <div ng-show="editState">
                <div>
                    <input type="text"/>
                    <button type="button" ng-click="item.toggle()">Cancel</button>
                </div>
            </div>
        `,
        link: function (scope, element, attrs) {
            scope.editState = false;

            //Here you add the function to the isolate scope 'item' variable
            scope.item.toggle = function () {
                scope.editState = !scope.editState;
            }
        }
    }
});