Angularjs 在angular js中将作用域传递给指令
我有以下带有简单http get的Angular控制器:Angularjs 在angular js中将作用域传递给指令,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我有以下带有简单http get的Angular控制器: angular.module('SCtrl', ['ng-fusioncharts']).controller('SCtrl', function($scope, $http) { //Get All errors. $http({ method: 'GET', url: 'node/errors' }).then(function successCallback(response
angular.module('SCtrl', ['ng-fusioncharts']).controller('SCtrl', function($scope, $http) {
//Get All errors.
$http({
method: 'GET',
url: 'node/errors'
}).then(function successCallback(response) {
$scope.notifications = response.data;
console.log($scope.notifications);
}, function errorCallback(response) {
});
}
这个很好用
然后我有以下指令。我希望能够将{{notifications}}放在模块主体中,但是它不起作用。我猜这是因为我给了指令自己的范围。链接作用域的正确方法是什么?我的尝试在下面,但这不起作用
谢谢
angular.module('SCtrl').directive('listDirective',function(){
return{
restrict:'E',
scope:{
listTitle:"@",
notifications:"="
},
templateUrl: '../templates/listWidget.html',
link: function(scope, element, attrs){
$scope.notifications=scope.notifications;
}
}
});
Mis uderstood最初,正如@devqon指出的,您需要在
列表指令中传递通知
对象,该对象可以在列表小部件.html中访问
<list-directive notifications="notifications"></list-directive>
Mis uderstood最初,正如@devqon指出的,您需要在列表指令中传递通知
对象,该对象可以在列表小部件.html中访问
<list-directive notifications="notifications"></list-directive>
链接:函数(范围、元素、属性){
scope.notifications=scope.notifications;
}
您应该分配值notifications=“notifications”,否则无法获取。或者,您可以在指令本身中触发API调用。现在您不需要分配通知=“通知”。
链接:函数(范围、元素、属性){
scope.notifications=scope.notifications;
}
您应该分配值notifications=“notifications”,否则无法获取。或者,您可以在指令本身中触发API调用。现在,您不需要分配notifications=“notifications”。如果您直接在listWidget.html
中使用通知,您只需执行
即可。如果您直接在listWidget.html
中使用通知,您只需执行
更好:)但不知道是否完成,因为OP似乎希望在链接函数中包含通知(未提及)reason@devqon:实际上不需要,这就是我最初感到困惑的地方。。。由于对象应在视图中可用。。。除非他想对汉克斯进行某种操纵。。。这是可行的,但是有没有一种方法我不需要在html中传递它?什么是最佳实践?@MattBoyle:通常你会传递值,如果你不想,那么你需要完全删除范围对象,这不会创建一个孤立的范围。。和直接访问控制器作用域更好:),但不知道是否完成,因为OP似乎希望在链接函数中为任何内容(未提及)设置通知reason@devqon:实际上不需要,这就是我最初感到困惑的地方。。。由于对象应在视图中可用。。。除非他想对汉克斯进行某种操纵。。。这是可行的,但是有没有一种方法我不需要在html中传递它?什么是最佳实践?@MattBoyle:通常你会传递值,如果你不想,那么你需要完全删除范围对象,这不会创建一个孤立的范围。。并直接访问控制器范围