简体   繁体   English

使用AngularJs从数据属性中检索数据

[英]Retrieving the data from data-attribute with AngularJs

I am working on a team planner manager where the user can create a tile(=task) when clicking on the body and dragging it around the canvas. 我正在一个团队计划经理中工作,用户可以在单击主体并将其拖动到画布周围时创建一个tile(= task)。 I am trying to link the created task to the correct user. 我正在尝试将创建的任务链接到正确的用户。 On the left of the screen I have a list of users. 在屏幕的左侧,我有一个用户列表。 Now the task are just placed random on the grid. 现在,任务只是随机放置在网格上。 The grids logic is made up of rows and columns. 网格逻辑由行和列组成。 So I thought I first determine the y postion of each user and then link the position on the task to the user. 所以我想我首先确定每个用户的y位置,然后将任务上的位置链接到用户。

I am working with Angular and Node to create this team planner 我正在与Angular和Node一起创建此团队计划者

Via a custom directive i obtained the user position (part of the code of my app.js file): 通过自定义指令,我获得了用户位置(我的app.js文件的代码的一部分):

$rootScope = {};
$rootScope.users = [];

contentEditor.directive('custom', function($document) {
    return {
        restrict: 'E',
        scope: true,
        link: function(scope, element, attrs) {  
            var mail = $(element[0]).attr('data-email');
            console.log("mail ", $(element).attr('data-email'));
            $rootScope.users.push({"top ":element[0].offsetTop, });
            console.log(scope)
            console.log("Y positon of a user circle " + element[0].offsetTop);
        }
    }
});

Part of the html code: html代码的一部分:

   <!-- START SIDEBAR -->
    <div id="sidebar" class="md-whiteframe-z4">
      <img id="logo" src="images/logo.png" alt="" >
      <div class="userList">
         <ul>
            <li ng-repeat="user in userInfo" id="userPos" class="circular" data-email="{{user.email}}"><p class="initials"><custom></custom>{{user.initials}}</p></li>
          </ul>
      </div>
    </div>
    <!-- END SIDEBAR -->

To get a better idea of my app check out this link -> http://gyazo.com/2e011b0e38bc1e36c198bbaa322ad06c 要更好地了解我的应用,请查看此链接-> http://gyazo.com/2e011b0e38bc1e36c198bbaa322ad06c

My question is how to access the data email from my data attribute (see list item). 我的问题是如何从我的数据属性访问数据电子邮件(请参阅列表项)。 Now it returns undefined when I try to access it. 现在,当我尝试访问它时,它返回未定义。

Firstly it looks like you are trying to access an attribute outside of the scope of your directive. 首先,看起来您正在尝试访问指令范围之外的属性。

While this could be possible with JQuery, it would be a bad idea as directives are supposed to only deal with the data within them. 尽管使用JQuery可以做到这一点,但这不是一个好主意,因为指令应该只处理其中的数据。 This means they can be reused anywhere in your application without having to rely on external data being set up in a certain way. 这意味着它们可以在应用程序中的任何地方重用,而不必依赖以某种方式设置的外部数据。

So instead of your current mark up. 因此,而不是您当前的标记。

<li ng-repeat="user in userInfo" id="userPos" class="circular" data-email="{{user.email}}">
     <p class="initials">
         <custom></custom>{{user.initials}}
     </p>
</li>

Use this, which puts the attribute where your directive can access it. 使用它,将属性放在您的指令可以访问它的位置。

<li ng-repeat="user in userInfo" id="userPos" class="circular">
    <p class="initials">
        <custom email="user.email"></custom>
        {{user.initials}}
    </p>
</li>

I think it's preferable to utilise the scope variable of a directive if you're only using the attribute in the link stage of a directive. 我认为,如果仅在指令的链接阶段使用属性,则最好利用指令的作用域变量。 So modify your directive like so. 因此,像这样修改指令。

Specifying '=' for the scope attribute means that if the email variable is updated outside of the scope of the directive (eg in your controller) then the changes will be reflected in the directive also. 为scope属性指定'='意味着如果在指令范围之外(例如,在您的控制器中)更新了电子邮件变量,则更改也将反映在指令中。

contentEditor.directive('custom', function($document) {
    return {
        restrict: 'E',
        scope: { email: '=' },
        link: function(scope, element, attrs) {  
            console.log("User email is: " + scope.email);
        }
    }
});

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

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