Javascript 通过控制器将数据传递到指令的角度
我使用angular 1.5来构建一个指令,可以在不同的情况下使用。我希望能够将数据作为对象从控制器传递到指令。我的设置如下: 控制器:Javascript 通过控制器将数据传递到指令的角度,javascript,angularjs,Javascript,Angularjs,我使用angular 1.5来构建一个指令,可以在不同的情况下使用。我希望能够将数据作为对象从控制器传递到指令。我的设置如下: 控制器: function ControllerWrapper($scope, $location, $window){ $scope.depth= 99; $scope.filter = {filters: [{column: 'stage', values: ['Closed']}]}; } HTML: 从我读到的过滤器:'='应该足以让数据通过
function ControllerWrapper($scope, $location, $window){
$scope.depth= 99;
$scope.filter = {filters: [{column: 'stage', values: ['Closed']}]};
}
HTML:
从我读到的
过滤器:'='
应该足以让数据通过,但在它尝试解析它之后,我得到一个空值。有什么建议吗?显示尝试解决此问题的代码。您知道您将隔离作用域编写为筛选器:'=?'
--这是故意的吗?看起来是错误的,可能是您的问题的根源…@anied我觉得=是从指令外部范围绑定到指令外部范围的正确语法。根据这句话:@chrismanes——是的,很抱歉不清楚——我认为等号应该是正确的,是问号让我停了下来。@anied好的,我把它拿出来了,我仍然有问题。所以在watch函数范围内。$eval(attributes.filters);正在解析为未定义。但是如果我注销console.log(attributes.filters);它给了我一串过滤器
<div ng-controller="ControllerWrapper">
<my-great-directive max-depth="{{depth}}" filters="filter"></my-great-directive>
</div>
return {
restrict: 'E',
templateUrl: function(element, attributes) {
return attributes.templateUrl;
},
link: function(scope, element, attributes) {
var obj = scope.$eval(attributes.filters);
var dir = scope.$eval(attributes.dir);
scope.$watch("filters",function(newValue,oldValue) {
if(scope.vm.dir != null){
scope.vm.dir.rows = scope.filterTreeGrid();
}
}, true);
},
controller: DirectiveController,
controllerAs: 'vm',
bindToController: true,
scope: {
maxDepth: '@',
filters: '=?'
}
};