gpt4 book ai didi

javascript - 范围内有什么以及属性变量/对象?

转载 作者:行者123 更新时间:2023-11-28 00:58:56 24 4
gpt4 key购买 nike

scopeattrs 变量中定义了什么?如何查看那里有什么?

此外,如何使用 scope 变量写入字段 IDscopeID

另外,为什么alert(scope.$id);一直指向001?我不是有2个应用程序吗?至少 2 个独立的实体?

    <script>
var firstApp = angular.module('firstApp', []);
firstApp.directive('myFirstApp', function() {
var variable = function(scope, element, attrs) {

alert(scope.$id);
$.each(elemArray, function(scope) {
$(this).on('click', function(scope) {
var id = $(this).attr("id");
scope.ID = $(this).attr("id");; // how to write to ID field here
alert($(this).attr("id"););
});
});
};
return {
restrict: 'AEC',
link: variable
};
});
</script>

<my-first-app>
<button id="a1">button 1</button>
<button id="a2">button 2</button>
<br />My ID: {{ ID }}
<br />My Scope ID: {{ scopeID }}
</my-first-app>
<br />
<my-first-app>
<button id="b1">button 1</button>
<button id="b2">button 2</button>
<br />My ID: {{ ID }}
<br />My Scope ID: {{ scopeID }}
</my-first-app>

最佳答案

Demo Plunker

如果您想查看指令的“范围”上的内容,可以使用 angular.extend 将范围的属性浅复制到另一个对象,然后将该对象绑定(bind)到您的 View :

指令

   app.directive('myFirstApp', function() {
return {
restrict: 'AEC',
link:function(scope, element, attr) {
var inScope = {};
angular.extend(inScope, scope);
scope.inScope = inScope;
scope.inAttr = attr;
}
}
})

HTML

<div my-first-app>
{{inScope }}
{{inAttr}}
</div>

关于javascript - 范围内有什么以及属性变量/对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25882257/

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