Angularjs-指令作用域
请参阅此小提琴以了解完整的示例: 我想将变量“enterKeyElement”的作用域隔离为指令redirectEnterKey、redirectEnterKeyTo和redirectEnterKeyFrom 但是,希望将变量“myInput”的作用域与“pageCtrl”共享 我能做这个吗?而不必在这些指令中引用“myInput”?这是最佳实践吗Angularjs-指令作用域,angularjs,Angularjs,请参阅此小提琴以了解完整的示例: 我想将变量“enterKeyElement”的作用域隔离为指令redirectEnterKey、redirectEnterKeyTo和redirectEnterKeyFrom 但是,希望将变量“myInput”的作用域与“pageCtrl”共享 我能做这个吗?而不必在这些指令中引用“myInput”?这是最佳实践吗 谢谢 我并不完全清楚您的问题,但我认为您可以从父范围创建隔离范围,并在需要时从父范围中包含一些属性,就像我已将myInput属性添加到隔离范围中一样
谢谢 我并不完全清楚您的问题,但我认为您可以从父范围创建隔离范围,并在需要时从父范围中包含一些属性,就像我已将myInput属性添加到隔离范围中一样,它将引用父范围的ng模型属性
.directive('redirectEnterKeyFrom', function() {
return {
scope:{myInput'=ngModel'}
restrict : 'A',
require : '^redirectEnterKey',
link : function($scope,$element) {
$element.keypress(function($event) {
if($event.keyCode == '13') {
$scope.enterKeyElement.click();
$event.stopPropagation();
$event.preventDefault();
}
});
}
}
})) 谢谢你试图帮助我,你给了我一些很好的线索。这是一个很好的角度范围界定教育 请原谅我不完整的问题。下面是问题的另一个例子: 注意,由于共享作用域,错误的输入被隐藏 这是我自己问题的答案:,对于任何有帮助的人来说 基本上,每个指令都有自己的作用域,并且可以通过父指令“redirectScopeExample”绑定到父控制器
var pageModule = angular.module('pageModule',[])
.controller('pageCtrl',['$scope',function($scope) {
}])
.directive('redirectScopeExample',function() {
return {
restrict : 'A',
scope : {
hideElement : '&',
redirectScopeExampleInput : '=ngModel'
},
controller : function($scope) {
}
}
})
.directive('redirectScopeExampleTo', function() {
return {
restrict : 'A',
require : '^redirectScopeExample',
link : function($scope,$element,$attr) {
$element.click(function() {
$scope.hideElement.toggle();
});
}
}
})
.directive('redirectScopeExampleFrom', function() {
return {
restrict : 'A',
require : '^redirectScopeExample',
link : function($scope,$element,$attrs) {
$scope.hideElement = $element;
}
}
});
我是否可以仅限制“enterKeyElement”的可视性,而不在这些指令中提及“myInput”?谢谢
var pageModule = angular.module('pageModule',[])
.controller('pageCtrl',['$scope',function($scope) {
}])
.directive('redirectScopeExample',function() {
return {
restrict : 'A',
scope : {
hideElement : '&',
redirectScopeExampleInput : '=ngModel'
},
controller : function($scope) {
}
}
})
.directive('redirectScopeExampleTo', function() {
return {
restrict : 'A',
require : '^redirectScopeExample',
link : function($scope,$element,$attr) {
$element.click(function() {
$scope.hideElement.toggle();
});
}
}
})
.directive('redirectScopeExampleFrom', function() {
return {
restrict : 'A',
require : '^redirectScopeExample',
link : function($scope,$element,$attrs) {
$scope.hideElement = $element;
}
}
});