Javascript 什么';范围内的属性,以及属性变量/对象?
在Javascript 什么';范围内的属性,以及属性变量/对象?,javascript,jquery,angularjs,object,Javascript,Jquery,Angularjs,Object,在范围和属性变量中定义了什么?怎么看那里有什么 另外,如何使用scope变量写入字段ID和scopeID 另外,为什么警报(范围$id)继续指向001?我没有两个应用程序吗?至少2个独立实体 <script> var firstApp = angular.module('firstApp', []); firstApp.directive('myFirstApp', function() { var variable =
范围
和属性
变量中定义了什么?怎么看那里有什么
另外,如何使用scope
变量写入字段ID
和scopeID
另外,为什么警报(范围$id)代码>继续指向001?我没有两个应用程序吗?至少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>
var firstApp=angular.module('firstApp',[]);
指令('myFirstApp',函数(){
var变量=函数(范围、元素、属性){
警报(范围$id);
$。每个(elemArray,功能(范围){
$(此).on('click',函数(范围){
var id=$(this.attr(“id”);
scope.ID=$(this.attr(“ID”);//如何写入此处的ID字段
警报($(this.attr(“id”););
});
});
};
返回{
限制:“AEC”,
链接:变量
};
});
按钮1
按钮2
我的ID:{{ID}
我的作用域ID:{{scopeID}
按钮1
按钮2
我的ID:{{ID}
我的作用域ID:{{scopeID}
如果要查看指令“范围”上的内容,可以使用angular.extend将范围的属性浅复制到另一个对象,然后将对象绑定到视图:
指令
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>
{{inScope}}
{{inAttr}}
有一个名为AngularJs的chrome插件,通过它你可以看到作用域中的内容。除非另有规定,否则作用域是继承的。因此,两个应用程序都继承了相同的父作用域。如果您希望每个指令都有自己的子作用域,请在指令定义中添加“scope:true”。我认为restrict:'A'
,应该是restrict:'AEC'
,这样才能起作用。