简体   繁体   English

Angular指令将范围隔离到父绑定未定义

[英]Angular directive isolate scope to parent binding undefined

I'm using (the awesome) Restangular and i'm running into something that forces me to use scope.$parent (not awesome), and i don't want to use that. 我正在使用(真棒)Restangular,并且遇到了一些迫使我使用scope.$parent (不很棒)的东西,但我不想使用它。 It seems even though my controller is the parent scope to my directive's scope, the = isolated scope binding is evaluated before my parent controller is executed. 似乎即使我的控制器是我指令范围的父作用域,也要在执行我的父控制器之前对=隔离范围绑定进行评估。

With the following HTML: 使用以下HTML:

<div ng-controller="myController">
    <div x-my-directive x-some-value="parentValue"></div>
</div>

And the following directive: 和以下指令:

myApp.directive("myDirective", function () {
    return {
        restrict: 'A',
        link: function (scope, elem) {
            console.log(scope.someValue); // Logs 'undefined' :(
        },
        scope: {
            someValue: "="
        }
    }
});

And the following controller: 和以下控制器:

myApp.controller("myController", function($scope, allMyValues) {
    allMyValues.getList().then(function(parentValue){
        $scope.parentValue = parentValue;
    });
}

As shown in my directives link function, evaluating a scope property that should have been bound to my parent's scope property returns undefined . 如我的指令link功能所示,评估应该绑定到我父母的scope属性的scope属性将返回undefined However when i change my directives link function to the following: 但是,当我将指令link功能更改为以下内容时:

    myApp.directive("myDirective", function () {
        return {
            restrict: 'A',
            link: function (scope, elem) {
               setTimeout(function() {
                  console.log(scope.someValue); // Logs '{1: number_1, 2: number_2}'
                }, 2000);
            },
            scope: {
                someValue: "="
            }
        }
    });

How do i go about resolving this?? 我该如何解决这个问题?

Thanks 谢谢

Looks like you are waiting for a promise to resolve before assigning the value to the scope. 在将值分配给作用域之前,您似乎正在等待承诺的解决。

There are a few ways you might handle this. 您可以通过几种方法来处理此问题。

One way is to try moving the Restangular call to a resolve function for the view which holds the controller. 一种方法是尝试将Restangular调用移至包含控制器的视图的resolve函数。 Then you get access to the resolved data directly as an injection in your controllers 然后,您可以直接注入控制器中来访问已解析的数据

Another way might be to just assign the promise directly to the scope and then in the linking function wait for a resolution. 另一种方法可能是将promise直接分配给作用域,然后在链接函数中等待解析。

scope.someValue.then(function(value) { console.log(value); });

that should helps: 这应该会有所帮助:

myApp.controller("myController", function($scope, allMyValues) {
//add this line
$scope.parentValue={};

    allMyValues.getList().then(function(parentValue){
        $scope.parentValue = parentValue;
    });
}

$scope.parentValue not exist until your request is resolved so add line like below to your code sample demo http://jsbin.com/komikitado/1/edit 在解决您的请求之前,$ scope.parentValue不存在,因此在代码示例演示http://jsbin.com/komikitado/1/edit中添加以下内容

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

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