AngularJS:指令无法访问隔离作用域对象
我试图在我的指令中添加一些带有隔离作用域的默认值。基本上,当我的指令被绑定时,我需要使用scope对象进行一些DOM操作。下面是我的代码: 控制器:AngularJS:指令无法访问隔离作用域对象,angularjs,angularjs-directive,angularjs-scope,angularjs-service,Angularjs,Angularjs Directive,Angularjs Scope,Angularjs Service,我试图在我的指令中添加一些带有隔离作用域的默认值。基本上,当我的指令被绑定时,我需要使用scope对象进行一些DOM操作。下面是我的代码: 控制器: angular.module('ctrl').controller('TempCtrl', function($scope, $location, $window, $timeout, RestService, CommonSerivce) { $scope.showAppEditWindow = function() { //Bindi
angular.module('ctrl').controller('TempCtrl', function($scope, $location, $window, $timeout, RestService, CommonSerivce) {
$scope.showAppEditWindow = function() {
//Binding the directive isolate scope objects with parent scope objects
$scope.asAppObj = $scope.appObj;
$scope.asAppSubs = $scope.appSubscriptions;
//Making Initial Settings
CommonSerivce.broadcastFunction('doDirectiveBroadcast', "");
};
服务:
angular.module('Services').factory('CommonSerivce', function ($rootScope) {
return {
broadcastFunction: function(listener, args) {
$rootScope.$broadcast(listener, args);
}
};
指令:
angular.module('directives').directive('tempDirective', function() {
return {
restrict : 'E',
scope:{
appObj:'=asAppObj',
appSubs: '=asAppSubs'
},
link : function(scope, element, attrs) {},
controller : function ($scope,Services,CommonSerivce) {
//Broadcast Listener
$scope.$on('doDirectiveBroadcast', function (event, args) {
$scope.setDefaults();
});
$scope.setDefaults = function() {
//Setting Default Value
alert(JSON.stringify($scope.appSubs)); //Coming as undefined
};
},
templateUrl:"../template.html"
};
});
自定义指令元素:
<temp-directive as-app-obj="asAppObj" as-app-subs="asAppSubs" />
现在的问题是,在尝试访问defaultmethodinside指令中的隔离作用域时,我得到了一个未定义的值,而数据即将到来并绑定到DOM。如何访问广播侦听器中的隔离作用域并修改指令模板HTML?是否还有另一个wasy用于处理此问题?问题是:此时angular尚未更新其绑定 您不应该像这样访问变量,尝试使用angular js绑定机制将其绑定到视图(例如使用$watch)。绑定到父范围变量意味着您是被动的,只需监听更改并更新其他变量或视图。这就是我们应该如何使用angular 如果您仍然需要访问它。您可以尝试使用$ 最好使用$watch
angular.module('ctrl', []).controller('TempCtrl', function ($scope, $location, $rootScope) {
$scope.appSubscriptions = "Subscriptions";
$scope.appObj = "Objs";
$scope.showAppEditWindow = function () {
//Binding the directive isolate scope objects with parent scope objects
$scope.asAppObj = $scope.appObj;
$scope.asAppSubs = $scope.appSubscriptions;
};
});
angular.module('ctrl').directive('tempDirective', function () {
return {
restrict: 'E',
replace: true,
scope: {
appObj: '=asAppObj',
appSubs: '=asAppSubs'
},
link: function (scope, element, attrs) {
},
controller: function ($scope, $timeout) {
$scope.$watch("appSubs",function(newValue,OldValue,scope){
if (newValue){
alert(JSON.stringify(newValue));
}
});
},
template: "<div>{{appSubs}}</div>"
};
});
angular.module('ctrl',[]).controller('TempCtrl',function($scope,$location,$rootScope){
$scope.appSubscriptions=“订阅”;
$scope.appObj=“Objs”;
$scope.showAppEditWindow=函数(){
//绑定指令隔离作用域对象与父作用域对象
$scope.asAppObj=$scope.appObj;
$scope.asAppSubs=$scope.appSubscriptions;
};
});
角度。模块('ctrl')。指令('tempDirective',函数(){
返回{
限制:'E',
替换:正确,
范围:{
appbj:'=asAppObj',
appSubs:'=asAppSubs'
},
链接:函数(范围、元素、属性){
},
控制器:函数($scope,$timeout){
$scope.$watch(“appSubs”,函数(newValue、OldValue、scope){
if(newValue){
警报(JSON.stringify(newValue));
}
});
},
模板:“{{appSubs}}”
};
});
通过使用$watch,在这种情况下,您不需要广播事件。在指令的控制器首次实例化时,隔离作用域变量很可能不可用,但在以下事件中可能需要它,例如:在绑定到ng click的函数内
这只是一个竞赛条件,并且对象并没有在指令的控制器加载时准确到达你能创建一个小提琴吗?@Akhilesh Aggarwal:你应该使用$watch。在@Khank To查看一个类似的问题:这甚至更好:)。为什么要在“链接”中添加“监视”定义。我测试过,它在“控制器”下也能工作。@Akhilesh Aggarwal:在你的情况下,我认为你应该在控制器内部使用$watch
controller
。因为控制器应该包含逻辑和链接功能,所以应该只关心在模型和应用程序之间建立动态连接view@KhanhTO:根据angular文档:“最佳实践:当您希望将API公开给其他指令时,请使用控制器。否则,请使用链接。”
angular.module('ctrl', []).controller('TempCtrl', function ($scope, $location, $rootScope) {
$scope.appSubscriptions = "Subscriptions";
$scope.appObj = "Objs";
$scope.showAppEditWindow = function () {
//Binding the directive isolate scope objects with parent scope objects
$scope.asAppObj = $scope.appObj;
$scope.asAppSubs = $scope.appSubscriptions;
};
});
angular.module('ctrl').directive('tempDirective', function () {
return {
restrict: 'E',
replace: true,
scope: {
appObj: '=asAppObj',
appSubs: '=asAppSubs'
},
link: function (scope, element, attrs) {
},
controller: function ($scope, $timeout) {
$scope.$watch("appSubs",function(newValue,OldValue,scope){
if (newValue){
alert(JSON.stringify(newValue));
}
});
},
template: "<div>{{appSubs}}</div>"
};
});