Angularjs 如何在控制器的隔离作用域中使用绑定值
我有个指示。我想在指令的控制器中使用该指令的属性值。我试图通过将属性值绑定到隔离范围来实现这一点。但是,我遇到了一个问题,属性值似乎没有立即绑定到隔离范围 考虑以下代码:Angularjs 如何在控制器的隔离作用域中使用绑定值,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我有个指示。我想在指令的控制器中使用该指令的属性值。我试图通过将属性值绑定到隔离范围来实现这一点。但是,我遇到了一个问题,属性值似乎没有立即绑定到隔离范围 考虑以下代码: angular.module('startup.directives.decision', []) .directive('decisionMaker', [function () { return{ restrict: 'E', templateUrl
angular.module('startup.directives.decision', [])
.directive('decisionMaker', [function () {
return{
restrict: 'E',
templateUrl: 'views/directives/decision.html',
scope: {
decisionType:"@",
},
controller: ['$scope', 'Decisions', function ($scope, Decisions){
//this prints undefined
console.log($scope.decisionType);
//this prints the proper value when called a couple seconds after page load
$scope.getDecisionType = function(){
console.log($scope.decisionType);
};
//this is my motivation for wanting $scope.decisionType to be bound immediately
if($scope.decisionType==='hire'){
//should do some stuff here
}
}]
};
}]);
我这样称呼我的指令:
<decision-maker decision-type="investment"></decision-maker>
<decision-maker decision-type="hire"></decision-maker>
controller: ['$scope', '$attrs', 'Decisions', function ($scope, $attrs, Decisions){
//this prints undefined
console.log($scope.decisionType);
//this prints the proper value when called a couple seconds after page load
$scope.getDecisionType = function(){
console.log($scope.decisionType);
};
$attrs.$observe('decisionType', function(value) {
//this is my motivation for wanting $scope.decisionType to be bound immediately
if($scope.decisionType==='hire'){
//should do some stuff here
}
});
}]
您需要使用
$observe
功能。请参见的属性部分
比如说:
<decision-maker decision-type="investment"></decision-maker>
<decision-maker decision-type="hire"></decision-maker>
controller: ['$scope', '$attrs', 'Decisions', function ($scope, $attrs, Decisions){
//this prints undefined
console.log($scope.decisionType);
//this prints the proper value when called a couple seconds after page load
$scope.getDecisionType = function(){
console.log($scope.decisionType);
};
$attrs.$observe('decisionType', function(value) {
//this is my motivation for wanting $scope.decisionType to be bound immediately
if($scope.decisionType==='hire'){
//should do some stuff here
}
});
}]
您需要使用
$observe
功能。请参见的属性部分
比如说:
<decision-maker decision-type="investment"></decision-maker>
<decision-maker decision-type="hire"></decision-maker>
controller: ['$scope', '$attrs', 'Decisions', function ($scope, $attrs, Decisions){
//this prints undefined
console.log($scope.decisionType);
//this prints the proper value when called a couple seconds after page load
$scope.getDecisionType = function(){
console.log($scope.decisionType);
};
$attrs.$observe('decisionType', function(value) {
//this is my motivation for wanting $scope.decisionType to be bound immediately
if($scope.decisionType==='hire'){
//should do some stuff here
}
});
}]
我可以通过$attrs对象更直接地访问属性,而不是通过将属性与作用域绑定来访问属性
angular.module('startup.directives.decision', [])
.directive('decisionMaker', [function () {
return{
restrict: 'E',
templateUrl: 'views/directives/decision.html',
scope: {},
controller: ['$scope', '$attrs', 'Decisions', function ($scope, $attrs, Decisions){
//this prints the correct value
console.log($attrs.decisionType);
if($attrs.decisionType==='hire'){
//should do some stuff here
}
}]
};
}]);
我可以通过$attrs对象更直接地访问属性,而不是通过将属性与作用域绑定来访问属性
angular.module('startup.directives.decision', [])
.directive('decisionMaker', [function () {
return{
restrict: 'E',
templateUrl: 'views/directives/decision.html',
scope: {},
controller: ['$scope', '$attrs', 'Decisions', function ($scope, $attrs, Decisions){
//this prints the correct value
console.log($attrs.decisionType);
if($attrs.decisionType==='hire'){
//should do some stuff here
}
}]
};
}]);
您如何调用指令(HTML)?调用指令的模板的范围是什么?嗨,Nikos,我认为这个问题应该独立于调用指令的范围。我唯一想得到的是指令的属性值。你有这个吗?我使用的是一个看似相同的案例,
$scope.decisionType
立即可用。这里有些东西很臭…不幸的是我不知道。虽然在控制器开始执行之前,如果你有一堆类似的东西将属性值连接到作用域,我很乐意看到它。是的,我几乎为此发疯了,谢谢:)它似乎不适用于Angular 1.1.1(但它适用于1.2(在我的例子中是1.1.4)().Yupi!如何调用指令(HTML)?调用指令的模板的作用域是什么?嗨,Nikos,我认为这个问题应该独立于调用指令的作用域。我试图得到的唯一东西是指令的属性值。你对此有意见吗?我使用的是一个似乎相同的情况,其中$scope.decisionType
可用很快就消失了。这里有些东西很臭……我不很遗憾。虽然在控制器开始执行之前,如果你有一把类似的东西将属性值连接到作用域,我很想看到它。是的,我几乎疯了。谢谢:)它似乎不适用于Angular 1.1.1(.但是它适用于1.2(在我的例子中是1.1.4)(.Yupi!)只要属性没有插值({}
)值,它就可以工作在属性的值中,$attrs
对象将为您提供未定义的属性。因此,$observe
函数的需要。这一点很好!但是我也遇到了$observe的问题…请参阅我对您答案的评论,只要您没有插值({}
)属性的值。如果要使用{{}
在属性的值中,$attrs
对象将为您提供未定义的。因此,$observe
函数的需要。这一点很好!但是,我也遇到了$observe的问题…请参阅我对您答案的评论。在ng repeat中使用此指令的情况下,此解决方案不起作用。但是,如果我使用$scope.$watch代替$attrs.$observe我成功了。请参阅此问题报告:在ng重复中使用此指令时,此解决方案不起作用。但是,如果我使用$scope.$watch代替$attrs.$observe我成功了。请参阅此问题报告: