简体   繁体   English

无法从Angular Directive中的服务访问对象属性

[英]Can't access object attributes from service in Angular Directive

I have a JSON service that outputs a roleName and an id and I am trying to use the role to hide or show elements in an application using an angular directive. 我有一个输出roleName和id的JSON服务,并且我试图使用该角色使用angular指令在应用程序中隐藏或显示元素。 It appears that the service is successfully being called and $scope.data is being populated from the output but I don't seem to be able to access the role from the data object as the console log reads Undefined for $scope.data.rolename. 似乎已成功调用该服务,并且从输出中填充了$ scope.data,但是由于控制台日志为$ scope.data.rolename读取了Undefined,我似乎无法从数据对象访问该角色。

.JS .JS

app.directive('restrictTo', ['SecuritySvc', function (SecuritySvc) {
return {
    restrict: 'EA',
    replace: true,
    transclude: true,
    scope: {},
    controller: ['$scope', '$attrs', '$q', 'SecuritySvc', function ($scope, $attrs, $q, SecuritySvc) {
        $scope.data = SecuritySvc.getRole();

            if ($scope.data.roleName == $attrs.restrictTo) {
                $scope.allowed = true;
            } else {
                $scope.allowed = false;
            }

            console.log($scope.data); // showing in log
            console.log($scope.data.roleName); // Undefined

    }],
    template: '<div ng-show="{{ $scope.allowed }}" ng-transclude></div>'
}

Console output from the code above 上面代码的控制台输出

$promise: Object
$resolved: true
roleId: 1
roleName: "Admin"

undefined

I think I'm a little confused on your purpose here. 我想我对你在这里的目的有些困惑。 You are creating a deferred promise and then immediately resolving it? 您是在创建一个延迟的承诺,然后立即解决它? Doesn't that defeat the purpose of the deferred/promise pattern's handling of asynchronous calls? 这是否违反了延迟/承诺模式处理异步调用的目的? Is your SecuritySvc.getRole() method doing an AJAX call to return that data? 您的SecuritySvc.getRole()方法是否执行AJAX调用以返回该数据? I had always thought that the AJAX call should be the thread calling Promise::resolve once it had completed. 我一直认为AJAX调用应该是在完成后调用Promise::resolve的线程。 Have you tried just using $http ? 您是否尝试过仅使用$http

Even after all that, I'm REALLY confused that you can console.log data successfully, but not data.roleName . 即使如此,我还是很困惑您可以成功地console.log data ,但不能data.roleName data showing up implies that whatever you're doing with the deferred/promise pattern, you're getting your response where you need it. 显示的data表明,无论您使用延迟/承诺模式做什么,都将在需要的地方得到响应。 Are you certain about the structure of the data object at this point? 您现在确定data对象的结构吗? There's no layer between data and your information? data和您的信息之间没有层次? I'm a little surprised to see it buried in with the Angular methods and properties like $promise and $resolved . 看到它被Angular方法和$promise$resolved类的属性所掩盖,我感到有些惊讶。

Don't know if any of that is helpful... 不知道这是否有帮助...

I've seen this behavior when your directive creates an isolated scope (as yours does) and the service is accessing the view controller (rather than the directive controller) scope. 当您的指令创建一个隔离范围(如您的操作)并且服务正在访问视图控制器(而不是指令控制器)范围时,我已经看到了这种行为。 Since the scopes are different, the $scope.data objects are different. 由于作用域不同,因此$scope.data对象也不同。

You can check the scope id numbers by placing a breakpoint within the directive controller and another within the service, and examine the $id values. 您可以通过在指令控制器中放置一个断点,在服务中放置另一个断点来检查作用域ID号,并检查$ id值。 There are several ways of working around this, but the specific solution will depend on your particular implementation. 有几种解决方法,但是具体的解决方案将取决于您的特定实现。

It might be helpful to read up on the isolated scope bindings - '@' and '=', see: 阅读隔离的范围绑定-'@'和'='可能会有所帮助,请参阅:

http://docs.angularjs.org/guide/directive http://docs.angularjs.org/guide/directive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM