如何获得angularjs中自定义标记的属性值?

时间:2022-03-28 20:34:08

I am trying to create a custom tag using angularJs. This tag has an attribute named data. data gets it value like this <skillviz data="{{user.info}}"></skillviz>. user.info is a JSON object. But when i try to access this data attribute in my directive definition, I get undefined. What is the correct way to do this ?

我正在尝试使用angularJs创建一个自定义标记。这个标记有一个名为data的属性。数据获取它的值如 。info是一个JSON对象。但是当我试图在指令定义中访问这个数据属性时,我就没有定义了。正确的做法是什么?

html code

html代码

<div id="info-box" ng-repeat="user in users | orderBy:orderProp">            
          <div id="skill-block">
            <skillviz height="50" data="{{user.skills}}"></skillviz>
          </div>
      </div>

users is a JSON type object, declared in the controller. So basically users will be a list(array) of

用户是一个JSON类型的对象,在控制器中声明。所以基本上用户将是一个列表(数组)。

{"first_name": "Tifanny",

        "last_name": "Maxwell",
        "skills": [
            {"name": "Java", "score": 4.8, "color" : "red"},
            {"name": "C++", "score": 4.0, "color" : "blue"},
        ]
    }, 

services.js

services.js

angular.module('yott', []).directive('skillviz', function () {
return {
    restrict: 'E',
    link: function (scope, element, attrs) {
        element.html("<script>alert(" + attrs['data'] + ")</script>");
        });
    }
  }
});

alert box pops up saying undefined

弹出警告框,表示未定义

2 个解决方案

#1


4  

Use $observe to observe changes to the attribute:

使用$observe观察对属性的更改:

attrs.$observe('data', function(value) {
  console.log('data has changed value to ' + value);
});

And $set to change value:

和$set to change value:

attrs.$set('data', 'new value');

Alternatively you can pass/link it into the directive scope using @ (bind a local scope), & (provides a way to execute an expression in the context of the parent scope) or = (set up bi-directional binding) – explained here

或者,您可以使用@(绑定一个本地范围)将它传递/链接到指令范围,&(提供一种在父范围上下文中执行表达式的方法)或=(设置双向绑定)—这里解释

angular.module('yott', []).directive('skillviz', function () {
    return {
        restrict: 'E',
        scope { data: "=data" },
        link: function (scope, element, attrs) {
            element.html("<script>alert(" +scope.data + ")</script>");
            });
        }
      }
    });

#2


20  

Let's say you have the following markup:

假设您有以下标记:

<div ng-controller="MyController" data-id="someCustomValue">
</div>

Now in your controller you can do the following to access data-id:

现在在控制器中,可以执行以下操作来访问数据id:

app.controller('MyController', function ($scope, $attrs) {
  console.log($attrs.id); // Prints 'someCustomValue'
});

#1


4  

Use $observe to observe changes to the attribute:

使用$observe观察对属性的更改:

attrs.$observe('data', function(value) {
  console.log('data has changed value to ' + value);
});

And $set to change value:

和$set to change value:

attrs.$set('data', 'new value');

Alternatively you can pass/link it into the directive scope using @ (bind a local scope), & (provides a way to execute an expression in the context of the parent scope) or = (set up bi-directional binding) – explained here

或者,您可以使用@(绑定一个本地范围)将它传递/链接到指令范围,&(提供一种在父范围上下文中执行表达式的方法)或=(设置双向绑定)—这里解释

angular.module('yott', []).directive('skillviz', function () {
    return {
        restrict: 'E',
        scope { data: "=data" },
        link: function (scope, element, attrs) {
            element.html("<script>alert(" +scope.data + ")</script>");
            });
        }
      }
    });

#2


20  

Let's say you have the following markup:

假设您有以下标记:

<div ng-controller="MyController" data-id="someCustomValue">
</div>

Now in your controller you can do the following to access data-id:

现在在控制器中,可以执行以下操作来访问数据id:

app.controller('MyController', function ($scope, $attrs) {
  console.log($attrs.id); // Prints 'someCustomValue'
});