如何在angular中只对某些变量使用父作用域,而对其他变量保持隔离
How to use parent scope only for some variables in angular, and keep the other variables isolated
mydirective是一个孤立的作用域指令。这是因为我不想将指令的所有逻辑暴露给指令之外的任何地方。但是我想在指令之外访问输入数据。
<div mydirective>
<input ng-model="data.input">
</div>
<div mydirective>
<input ng-model="otherdata.public">
<input ng-model="more.than.one">
</div>
{{data.input}}
{{otherdata.public}}
我更喜欢HTML不改变就能工作,只改变指令代码。所以我想知道如何创建指令
app.directive('mydirective',function(){ return {
scope:true,
controller:function($scope){
$scope.this_variable_needs_to_be_private=true
},
transclude:true
}})
编辑:add translude:true。但是我仍然没有答案
考虑使用$transclude函数并使用$scope.$new()
:
(function() {
"use strict";
angular.module("myApp", [])
.controller("Controller1", ['$scope', Controller1])
.directive("mydirective", [mydirective]);
function Controller1($scope) {
$scope.data = {
input: 'data.input'
};
$scope.otherdata = {
public: 'otherdata.public'
};
$scope.more = {
than: {
one: 'more.than.one'
}
}
}
function mydirective() {
function _link($scope, $element, $attrs, controller, $transclude) {
var childScope = $scope.$new(); //create a childScope
//$scope.this_variable_needs_to_be_private = true; //<-- doing this would add it to public parent scope
childScope.this_variable_needs_to_be_private = true; //<-- this puts it only on this directive's childscope
// See: https://docs.angularjs.org/api/ng/service/$compile#transclusion
$transclude(childScope, function(clone, scope) { //transcluding with our childScope
$element.append(clone); //appending the clone of our content;
});
}
return {
transclude: true,
link: _link
};
}
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
<div ng-app="myApp" ng-controller="Controller1">
<div mydirective>
<input ng-model="data.input">
<br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}
</div>
<br />
<div mydirective>
<input ng-model="otherdata.public">
<input ng-model="more.than.one">
<br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}
</div>
<hr />
<strong>data.input:</strong> {{data.input}}
<br /><strong>otherdata.public:</strong> {{otherdata.public}}
<br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}
</div>
$transclude
的进一步阅读:https://stackoverflow.com/a/13184889/446030.
要访问指令的隔离作用域变量,可以使用单向或双向数据绑定,如下所示:
app.directive('mydirective',function(){ return {
scope:{var1:"=outerVar"},
controller:function($scope){
$scope.var1='I am accessible outside...';
}
}})
子作用域的变量无论如何在父作用域中是不可见的。您需要在隔离作用域中使用父作用域的变量来在它们之间进行通信。
相关文章:
- 带有KendoUI组件的Javascript变量作用域
- Javascript变量作用域:从回调函数中提取变量
- 使用JavaScript回调函数了解变量作用域和闭包
- jquery.post和变量作用域
- 变量作用域:在函数之间传递的名称
- 在 AJAX 和 Javascript 中使用变量和变量作用域
- 节点.js/express.js中的变量作用域
- jquery ajax 回调变量作用域(为什么有些在作用域中,而另一些则不在作用域中)
- JavaScript 中的变量作用域
- ajax GET 和 POST 中的变量作用域
- Javascript:变量作用域和全局变量的弊端
- Javascript 自执行函数和变量作用域
- 拼接上的Javascript全局变量作用域/覆盖
- 在Node中与jsdom和jquery共享变量作用域
- NodeJS:代码优化和变量作用域
- Javascript数组变量作用域
- AngularJS ng控制器指令不接受javascript中的变量(作用域函数),也不给出任何错误
- 回调函数中的变量作用域呢
- JavaScript/jQuery变量作用域导致错误
- AJAX变量作用域