javascript - 如何使 "ng-"与任何属性一起使用?

标签 javascript angularjs

ng-enabled, ng-src, ng-class 有什么通用的东西可以让它适用于任何属性 (ng-x)?

例如,这是我的代码:

<div ng-attrbuite1="a" ng-otherattribute="b"></div>

我想要它设置:

<div attribute1="a_value" attribute2="b_value_in_scope">

在运行时我还不知道,属性的名称将存在于 HTML 中。

最佳答案

您可以使用指令。这是一个简单的例子:

HTML

<div ng-app="myApp">
  <div ui-foo="foo">foo scope variable: {{foo}}</div>
</div>

JS

angular.module('myApp', []).directive('uiFoo',
  function() {
    return {
      restrict: 'EAC',
      link: function($scope, element, attrs, controller) {
        $scope.foo = attrs.uiFoo;
      }
    };
  }
);

关于javascript - 如何使 "ng-"与任何属性一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970472/

相关文章:

javascript - $http.post -> start (用于预加载器)

angularjs - 移动应用程序中的 Cookie

Javascript 私有(private)函数和 object.prototype 扩展

javascript - 包装 jQuery 集合并附加到类

javascript - 如何在 CasperJS 断言失败时继续测试用例?

javascript - Express、AJAX - res.send 不发送任何数据

javascript - 简单功能在 Chrome 57.0.2977.98(64 位)中不起作用

javascript - 添加新元素后正确向下滚动

javascript - AngularJS - <select> 标签中的下拉菜单问题,使用 ng-option 时不能有 2 个自定义选项

javascript - Angularjs + 使用 $q 的两个 http 请求