简体   繁体   English

属性双向绑定时AngularJS未定义对象

[英]Angularjs undefined object when attribute two-way binding

I am running into a problem with something I'm trying to do in AngularJS. 我在AngularJS中尝试执行的操作遇到了问题。 The thing goes like this: 事情是这样的:

I have a controller and I have a directive defined like this: 我有一个控制器,并且有一个这样定义的指令:

helpers.directive('someEvent', function() {

    ...
    scope: {
       api: '='
    }
    ...
    controller: ['$scope', function($scope) {

        $scope.hasSomeEventOccured = function(){

            return booleanVariable
        };

        $scope.api = {
           hasSomeEventOccured: $scope.hasSomeEventOccured
       }
    }]

});

Then, in some other controller I want to access that function hasSomeEventOccured ... the controller is defined as follows: 然后,在其他控制器中,我要访问该函数hasSomeEventOccured ...该控制器的定义如下:

moduleName.controller('moduleSomethingController',
['$scope', '$state', 'moduleRepository', function ($scope, $state, moduleRepository) {

    $scope.theEventOccured = $scope.someEventApi.hasSomeEventOccured();

}]);

And in the cshtml file I have: 在cshtml文件中,我有:

<some-event api="someEventApi" ></some-event>

<div ng-if="theEventOccured"></div>

The error that occasionally occurs is that $scope.someEventApi is undefined. 偶尔发生的错误是$scope.someEventApi未定义。 And so this line breaks: $scope.theEventOccured = $scope.someEventApi.hasSomeEventOccured(); 因此,此行中断了: $scope.theEventOccured = $scope.someEventApi.hasSomeEventOccured(); I assume this happens because the call to hasSomeEventOccured within the moduleSomethingController occurs before the binding with someEvent is completed. 我认为发生这种情况是因为对hasSomeEventOccuredmoduleSomethingController发生在与someEvent绑定完成之前。

My question is how do you solve this problem? 我的问题是您如何解决这个问题?

I am aware that in a directive you would have something like: 我知道,在指令中您将具有以下内容:

link: function(scope, element, attrs) {
   attrs.$observe(...);
}

But how can I achieve something like wait for the binding to complete in my situation? 但是如何在我的情况下实现诸如等待绑定完成之类的功能?

SO- The reason you get "$scope.someEventApi is undefined" error, is because you didn't set it anywhere. 如此-之所以收到“未定义$ scope.someEventApi”错误,是因为您没有在任何地方进行设置。 it is undefined! 它是不确定的! Everything you use inside the controller should be either defined (var x = something...) or injected (just like you injected $scope in your example) 您在控制器内部使用的所有东西都应该被定义(var x = something ...)或注入(就像您在示例中注入$ scope一样)

The way i see it, you have two options to get what you want: 我的看法是,您有两种选择来获得想要的东西:

  1. a common and straightforward way will be to define your function inside the controller and then send that function to the directive. 一种常见而直接的方法是在控制器内部定义函数,然后将该函数发送到指令。 that means you'll need to add your directive definition something like that: 这意味着您需要添加以下指令定义:

scope: {theEventOccured: '&' }

the & sign means it's a function. &符号表示它是一个功能。

inside your controller: 在您的控制器内:

$scope.theEventOccured = function() { /* your code here */ }

this way- the API is actually defined inside your controller, and the directive uses it when needed. 这样-API实际上是在您的控制器中定义的,该指令在需要时使用它。

  1. for more complex stuff- you can create a .factory and bind it to your controller and directive. 对于更复杂的东西,您可以创建一个.factory并将其绑定到您的控制器和指令。

  2. if you are trying to communicate between different controllers (or controllers of directive) you can either use a factory or $broadcast an event. 如果您尝试在不同的控制器(或指令的控制器)之间进行通信,则可以使用工厂事件或$ broadcast事件。 see $broadcast and $emit in the scope api 请参阅作用域api $ broadcast和$ emit

two other important things: usually you are not supposed to use a directive's function in it's parent controller scope if i understand what you want to achieve, you do not need $observe here. 还有两点重要的事情:通常,如果我了解要实现的目标,则不应在父控制器作用域中使用指令的功能,这里不需要$ observe。

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

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