如何使用angularjs单击指令中的特定项目

时间:2014-08-05 05:50:39

标签: html angularjs angularjs-directive

您好我想在下面的指令模板中使用id点击特定项目是我的指令

app.directive('dropdownmenu', function($window) {
    return {

         restrict: 'AE',
            templateUrl: 'app/partials/dropdownmenutemplate.html',
            link: function($scope, element, attr, ctrl) {
                $scope.click=function(e)
            {
                 var id = e.target.id; 
                 console.log("id value is"+id);
                }

            }
    };
});

以下是我的dropdownmenutemplate.html

<div class="ss-example">
        <div class="ss-btn-toolbar" style="margin: 0;">
            <div class="ss-btn-group">
                <button class="ss-btn ss-dropdown-toggle" data-toggle="dropdown">Action <span class="ss-caret"></span></button>
                <ul  class="ss-dropdown-menu">
                 <li id="Action" ng-model = "action1" ng-click="click()"><a href="#" >Action</a></li>
                <li id ="Another" ng-click="click()"><a href="#">Another action</a></li>
                <li id ="Something" ng-click="click()"><a href="#">Something else here</a></li>

                    <li class="ss-divider"></li>
                    <li><a href="#">Separated link</a></li>
                </ul>
            </div><!-- /ss-btn-group -->

        </div><!-- /ss-btn-toolbar -->
    </div>

当我点击&#34;动作&#34;它应该转到链接中的某个功能和&#34;动作&#34;应该打印我已经尝试过element.bind和watch函数element.bind正在为所有元素工作我需要它用于特定项目并且手表不工作&#34;, 请建议我如何做到这一点。

1 个答案:

答案 0 :(得分:0)

因为您正在调用ng-click =“click()”

尝试

pp.directive('dropdownmenu', function($window) {
    return {

         restrict: 'AE',
            templateUrl: 'app/partials/dropdownmenutemplate.html',
            link: function(scope, element, attr, ctrl) {

               scope.click = function(){
                             alert("inside click function");
                 };
                scope.$watch('action',function(newVal){
                    alert("value changed");
                });


            }
    };
});