传递字符串直接到角指令? [英] Pass string straight to angular directive?

查看:88
本文介绍了传递字符串直接到角指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何在角指令工作。在 angularjs.org ,值在JavaScript中的作用域设置,然后匹配时范围引用指令。

I'm trying to understand how to work with directives in Angular. In the examples on angularjs.org, values are set in the scope in JavaScript, and then that scope is referenced when matching a directive.

模板:

  <my-customer info="naomi"></my-customer>

适用范围:

  .controller('Ctrl', function($scope) {
    $scope.naomi = { name: 'Cat', address: '1600 Amphitheatre' };
    $scope.igor = { name: 'Dog', address: '123 Somewhere' };
  })

模板:

Name: {{customerInfo.name}} Address: {{customerInfo.address}}

我想通过属性,直接从模板。因此,在这个例子中,我想编写输出模板:

I would like to pass the attribute, straight from the template. So in this example I would like to write a template that outputs:

Name: Naomi

而无需通过范围去。

Without going through a scope.

推荐答案

您可以做到这一点使用的 ATTRS 参数://文档.angularjs.org /引导/指令相对=nofollow>链接功能

You can do that using the attrs argument of the linking function.

module.directive('name', function () {
  return {
    link: function (scope, element, attrs) {
      console.log(attrs.info);
      // <- 'naomi'
    }
  }
});

然后在标记:

<name info='naomi'></name>

这篇关于传递字符串直接到角指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆