gpt4 book ai didi

angularjs - Angular 指令 : multiple values from directive to scope

转载 作者:行者123 更新时间:2023-12-01 10:44:44 25 4
gpt4 key购买 nike

我想将一个指令的属性的多个值公开给 $scope。指令是动态生成的,如下例所示:

<my-directive first-value="foo" second-value="bar" third-value="foobar"></my-directive>

我需要 $scope 中的值,以便将它们提供给模板并使用它们。

最佳答案

简单...:-)

var app = angular.module('app', []);
app.controller('myCtrl', function ($scope) {});
app.directive('myDirective', function() {
return {
restrict: 'E',
template: '<p>myDirective:</p>{{firstValue}}, {{secondValue}}, {{thirdValue}}',
scope: {
firstValue: '@',
secondValue: '@',
thirdValue: '@'
},
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="myCtrl">
<my-directive first-value="foo" second-value="bar" third-value="foobar"></my-directive>
</div>
</div>

但你真的应该尝试自己编写这种代码,下次......:-)

关于angularjs - Angular 指令 : multiple values from directive to scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27836365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com