简体   繁体   English

AngularJS:范围变量在控制器内未定义

[英]AngularJS: Scope variable undefined within controller

I create a new Scope within a directive like this 我在这样的指令中创建一个新的作用域

controllersModule.directive('uploadBox', function() {
return {
    restrict: 'E',
    scope: {
        topic: '=topic',
        label: '=label'
    },
    templateUrl: '/assets/directives/uploadBox.html'
};});

And use this directive like this (both values -label and topic- are values, which are stored in JSON files, within two different controllers) 并像这样使用此指令(值-label和topic-都是值,它们存储在两个不同控制器中的JSON文件中)

<upload-box label="lc.getTerm('system_upload')" topic="tc.currentTopic.uID"></upload-box> 

So the attribute label and topic are both passed to the new (created) Scope. 因此,属性标签和主题都传递给新的(创建的)范围。 Now, the funny thing is, while I'm able to access both values within the template HTML file (uploadBox.html) 现在,有趣的是,虽然我能够访问模板HTML文件(uploadBox.html)中的两个值

<div ng-controller="DropUploadCtrl">
    <button class="btn btn-success" dropzone="dropzoneConfig">
        <!-- label is correctly shown -->
        {{ label }}
    </button>

    <!-- this link works fine: Evaluates to something like aaa/54dg54...SHA128-Hash...G4FX-->
    <a ng-href="aaa/{{ topic }}">BB</a>
</div>

Only the label value is accessible from within the DropUploadController. 从DropUploadController内部只能访问标签值。

controllersModule.controller('DropUploadCtrl', ['$scope',function($scope) {
var that = this;

console.log($scope.label); // this is working fine
console.log($scope.topic); // this is undefined

// some more stuff here

}]);

I know that scopes are created for specific controllers and you cannot share scope variables directly between two controllers. 我知道范围是为特定控制器创建的,您不能直接在两个控制器之间共享范围变量。 However, I don't think that this is a problem here because I can use the label value. 但是,我认为这不是问题,因为我可以使用标签值。 But, where am I wrong respectively what is wrong in my code? 但是,我分别在哪里错代码中的错误呢?

Thank you very much 非常感谢你

EDIT: 编辑:

The output for the rootScope within the controller 控制器内rootScope的输出

console.log("$scope::>"); 
console.log($scope.label); 
console.log($scope.topic); 
console.log("$rootScope::>") 
console.log($rootScope.label); 
console.log($rootScope.topic); 

looks like this: 看起来像这样:

"$scope::>" 
"I'm a label" 
undefined 
"$rootScope::>" 
undefined 
undefined 

Furthermore, the value is undefined in the link function of the directive 此外,该值在指令的链接函数中未定义

Edit2: The solution has been found! Edit2:已找到解决方案! The value in tc.currentTopic.uID was not filled at the moment the other controller was initializing. 另一个控制器初始化时,tc.currentTopic.uID中的值未填充。 That's why the expression {{topic}} was working fine because it has been evaluated again, when the value has changed, a couple of ms later. 这就是为什么表达式{{topic}}可以正常工作的原因,因为它的值在几毫秒后又被更改了,因此再次对其求值。 I fetch the data now directly via $routeParams. 我现在直接通过$ routeParams获取数据。

Directives have isolated scopes. 指令具有孤立的范围。 Since you added: 由于您添加了:

 scope: {
        topic: '=topic',
        label: '=label'
    },

it created an isolated scope. 它创建了一个孤立的范围。 But, angular documentation says: = or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. 但是,角度文档说: ==attr在本地范围属性和通过attr属性的值定义的名称的父范围属性之间建立双向绑定。 If no attr name is specified then the attribute name is assumed to be the same as the local name. 如果未指定attr名称,则假定属性名称与本地名称相同。 Given <widget my-attr="parentModel"> and widget definition of scope: { localModel:'=myAttr' } , then widget scope property localModel will reflect the value of parentModel on the parent scope. 给定<widget my-attr="parentModel">和的范围小部件定义: { localModel:'=myAttr' }则小部件范围属性localModel将反映的值parentModel上的父范围。 Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel . parentModel任何更改都将反映在localModel ,对localModel任何更改都将反映在parentModel

Link here 连结这里

First thing, if you run console.log(scope.label); 首先,如果您运行console.log(scope.label); in your directive's link function (which you don't have at the moment, you'd have to add it), is it getting a value? 在指令的链接函数中(您现在还没有,必须添加它),它是否具有价值?

Secondly, it looks like you want the directive to change a value in the parent controller's scope. 其次,您似乎希望指令更​​改父控制器范围内的值。 Sometimes you can get strange things happening when you do this. 有时,执行此操作时可能会发生奇怪的事情。 In this answer , it recommends wrapping those values in your controller in an object. 此答案中 ,建议将这些值包装在控制器中的一个对象中。

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

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