首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS指令-使用该指令时,按住ng键并单击不起作用

AngularJS指令-使用该指令时,按住ng键并单击不起作用
EN

Stack Overflow用户
提问于 2018-03-29 01:00:53
回答 1查看 53关注 0票数 1

我的新指令遇到了一个问题。当单击ng-click时,变量不会被实现。我在这里找到了一些答案,但它们都调用了一个方法。我只是在改变两个变量的值,当有点击的时候。

指令:

代码语言:javascript
复制
pmcApp.directive('sorting', function() {
    return {
        restrict : 'AECM',
        templateUrl : 'partials/directives/sorting.html',
        replace : true,
        scope : {
            title : "@",
            field : "@"
        }
    }
});

TemplateURL:

代码语言:javascript
复制
<a href="" ng-click="sortType = '{{field}}'; sortReverse = !sortReverse;"> 
    {{title}}
    <span ng-show="sortType == '{{field}}' && !sortReverse"                        
      class="glyphicon glyphicon-sort-by-attributes font-size-10"></span> 
    <span ng-show="sortType == '{{field}}' && sortReverse"
      class="glyphicon glyphicon-sort-by-attributes-alt font-size-10"></span>
</a>

html

代码语言:javascript
复制
<!-- Using directive - Not work when we click -->
<td><sorting title="First Name" field="firstName"></td>
<td><sorting title="Last Name" field="lastName"></td>
<!-- Not using directive - work when we click -->
<td>
    <a href="" ng-click="sortType = 'username'; sortReverse = !sortReverse;"> 
        Email 
        <span ng-show="sortType == 'username' && !sortReverse"                            class="glyphicon glyphicon-sort-by-attributes font-size-10"></span> 
        <span ng-show="sortType == 'username' && sortReverse"                            class="glyphicon glyphicon-sort-by-attributes-alt font-size-10"></span>
    </a>
</td>

浏览时生成的代码对于所有TD几乎是相同的,如下所示

代码语言:javascript
复制
<td>
    <a href="" ng-click="sortType = 'firstName'; sortReverse = !sortReverse;" title="First Name" field="firstName" class="ng-binding ng-isolate-scope"> 
        First Name 
        <span ng-show="sortType == 'firstName' &amp;&amp; !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10 ng-hide"></span> 
        <span ng-show="sortType == 'firstName' &amp;&amp; sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
    </a>
</td>

<td>
    <a href="" ng-click="sortType = 'lastName'; sortReverse = !sortReverse;" title="Last Name" field="lastName" class="ng-binding ng-isolate-scope"  > 
        Last Name 
        <span ng-show="sortType == 'lastName' &amp;&amp; !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10 ng-hide"></span> 
        <span ng-show="sortType == 'lastName' &amp;&amp; sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
   </a>
</td>

<td>
    <a href="" ng-click="sortType = 'username'; sortReverse = !sortReverse;"> 
        Email 
        <span ng-show="sortType == 'username' &amp;&amp; !sortReverse" class="glyphicon glyphicon-sort-by-attributes font-size-10"></span> 
        <span ng-show="sortType == 'username' &amp;&amp; sortReverse" class="glyphicon glyphicon-sort-by-attributes-alt font-size-10 ng-hide"></span>
    </a>
</td>

当我们点击电子邮件时,它可以正常工作。但是当我们点击使用指令创建的名字或姓氏时,它不起作用。

EN

回答 1

Stack Overflow用户

发布于 2018-03-29 01:41:51

sorting指令没有更改父作用域的属性。您正在修改sorting指令的本地作用域,而不是像电子邮件那样的父作用域。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49540300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档