简体   繁体   English

Angular.js自定义指令仅应用于第一个自定义元素

[英]Angular.js Custom Directive applied to only the first custom element

I'm trying to learn angular, but I've run in to a problem I don't quite understand using a custom directive. 我正在尝试学习角度,但是遇到了一个我不太了解的使用自定义指令的问题。

I have a simple controller: 我有一个简单的控制器:

eventsApp.controller('PanelLayoutController',
function PanelLayoutController($scope){
   $scope.rows = 2;
   $scope.cols = 0;

   $scope.addRow = function()
   {
       if($scope.rows < 8)
       {
           $scope.rows++;
       }
   };
   $scope.removeRow = function()
   {
       if($scope.rows > 1 )
       {
            $scope.rows--;
       }
   };

    $scope.addCol = function()
    {
        if($scope.cols < 8)
        {
            $scope.cols++;
        }
    };
    $scope.removeCol = function()
    {
        if($scope.cols > 1 )
        {
            $scope.cols--;
        }
    };

  }
 );

A custom directive: 自定义指令:

eventsApp.directive('upvote', function(){
    return{
        restrict:'E', // restrict to Element
        templateUrl:'/templates/directives/upvote.html',
        scope:{
            upvote:"&", // & binds local scope to function in parent scope
            downvote:"&",
            count:"@",
            title:"@"
        }
    }
});

And an html template (/templates/directives/upvote.html) 和一个html模板(/templates/directives/upvote.html)

<div class="span0 well votingWidget">
    <p class="well-title">{{title}}</p>
    <div class="votingButton" ng-click="upvote()">
        <i class="icon-chevron-up icon-white"></i>
    </div>
    <div class="badge badge-inverse">
        <div>{{count}}</div>
    </div>
    <div class="votingButton" ng-click="downvote()">
        <i class="icon-chevron-down"></i>
    </div>
</div>

In my main html page: 在我的主要html页面中:

<div ng-controller="PanelLayoutController">

      <upvote upvote="addRow()" downvote="removeRow()" count="{{rows}}" title="Rows"/>

      <upvote upvote="addCol()" downvote="removeCol()" count="{{cols}}" title="Cols" />

    </div>

The first upvote element (Rows) displays fine, however the second element and noting after that is rendered. 第一个upvote元素(行)显示良好,但是呈现第二个元素并在此之后进行注释。

Does this have anything to do with using an isolated scope on my directive and having two elements using the same directive? 这与在我的指令上使用隔离范围以及使两个元素使用同一指令有什么关系吗?

You cannot use self-closing html elements. 您不能使用自动关闭的html元素。 Try this fiddle http://jsfiddle.net/HQ6ee/ 试试这个小提琴http://jsfiddle.net/HQ6ee/

The only change I made is this: Instead of 我所做的唯一更改是:

<upvote .... />

I changed to 我改为

<upvote ...></upvote>

Related issue in Angular Bug Stream: https://github.com/angular/angular.js/issues/1953 Angular Bug Stream中的相关问题: https : //github.com/angular/angular.js/issues/1953

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

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