【问题标题】:Using a promise for retrieving data inside angular directive使用 Promise 在 Angular 指令中检索数据
【发布时间】:2014-02-01 13:49:21
【问题描述】:

我正在根据用户角色从 api 隐藏或显示元素。当我在代码中设置 data.roleName 时,该指令有效,但是当我尝试通过我的服务设置它时,我需要在加载指令的其余部分之前解决一个承诺,尽管我不断收到“无法读取未定义错误的属性这是代码.

.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) {
        var defer = $q.defer();
        defer.promise.then(function ($scope, SecuritySvc) {
            $scope.data = SecuritySvc.getRole();
        });
        defer.resolve();

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



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

.html

<div restrict-to="customer">
        <div class="hero-unit">
            <h1>Welcome!</h1>
            <p>Hello, valued customer</p>
        </div>
    </div>
    <div restrict-to="Admin">
        <div class="hero-unit">
            <h1>Admin Tools</h1>
            <p>This shouldn't be visible right now</p>
        </div>
    </div>

【问题讨论】:

    标签: javascript angularjs factory promise


    【解决方案1】:

    如果你不想使用 Q/defer 并且正在使用 $resource 你可以这样做:

    app.directive('restrictTo', ['SecuritySvc', function (SecuritySvc) {
    return {
        restrict: 'AE',
        replace: true,
        transclude: true,
        scope: {},
        controller: ['$scope', '$attrs',  'SecuritySvc', function ($scope, $attrs, SecuritySvc) {
            $scope.allowed = false;
            SecuritySvc.getMyRole().$promise.then(function (data,attrs) {
                if (data.roleName == $attrs.restrictTo) {
                    $scope.allowed = true;
                }  else {
                    $scope.allowed = false;
                }
            });
        }],
        template: '<div ng-show="allowed" ng-transclude></div>'
    };
    

    }]);

    【讨论】:

    • 你需要像那样将 SecuritySvc 注入指令和控制器吗?
    【解决方案2】:

    不确定您的 SecuritySvc 是什么或返回什么。我认为你应该这样做:

        var defer = $q.defer();
        defer.resolve(SecuritySvc.getRole());
        defer.promise.then(function (data) {
            $scope.data = data;
            if ($scope.data.roleName == $attrs.restrictTo) {
                $scope.allowed = true;
            } else {
                $scope.allowed = false;
             }
            console.log($scope.data);
        });
    

    【讨论】:

    • SecuritySvc 返回一个带有 id 和 roleName 的 json 对象。这很有效,我可以在浏览器控制台中看到该对象,但现在我无法访问该对象的角色名称和 ID。
    • 您是否收到任何错误消息?也许您可以发布控制台输出。
    • 输出中没有错误,但似乎没有将数据视为对象我添加了一行来诊断问题 'console.log($scope.data.roleName)' 控制台输出说它是未定义的。 '$scope.data' 的控制台输出是' $promise: Object $resolved: true roleId: 1 roleName: "Admin"'
    • 这比其他答案好吗?
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2023-04-02
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多