像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/