简体   繁体   English

为什么$ scope变量里面的指令没有得到更新?

[英]why $scope variable inside directive is not getting updated?

I have made a directive for highmaps using angular, map is getting rendered. 我已经为使用角的高图做了指令,正在渲染地图。 I need to pass the final configured object back to controller. 我需要将最终配置的对象传递回控制器。 Hence i am assigning like this in directive, 因此,我在指令中这样分配,

  $timeout(function() {
      scope.mapconfigured = mapConfig;
   });

Initially i defined the controller like this, 最初,我这样定义控制器,

app.controller('mainCtrl', function($scope, DB) {
    $scope.mydata = DB.getStatesData();
    $scope.mapconfigured = {};
}

But the mapconfigured variable is not getting updated inside the directive and also the controller is not printing what i exactly needed. 但是mapconfigured变量没有在指令中更新,并且控制器也没有打印我确切需要的内容。

I am also initially assigning the mapconfigured variable to the directive 我也最初是将mapconfigured变量分配给指令

  <my-map mapconfigured="mapconfigured" mydata="mydata" header="'Highmap Demo'"></my-map>

what is the issue here? 这是什么问题? Here is the APPLICATION 这是应用程序

EXPECTED OUTPUT: 预期的输出:

I need to assign the mapConfig object to mapconfigured variable of scope inside my controller. 我需要将mapConfig对象分配给控制器内部范围的mapconfigured变量。

That because you used mapconfigured in scope directive, and in the link directive you tried to define mapConfigured with data. 那是因为您在作用域指令中使用了mapconfigured ,而在链接指令中您试图用数据定义mapConfigured

Replace with this code: 替换为以下代码:

  $timeout(function() {
      scope.mapconfigured = mapConfig;
  });

As Ivan mentioned in comments it was a typo , scope variable i declared in the controller as 'mapconfigured' but in directive i assigned to 'mapConfigured', changing like this worked 正如Ivan在评论中提到的那样,它是一个错别字,作用域变量我在控制器中声明为“ mapconfigured”,但在指令i中却分配给了“ mapConfigured”,因此像这样工作

<my-map mapconfigured="mapconfigured" mydata="mydata" header="'Highmap Demo'"></my-map>

$timeout(function() {
   scope.mapconfigured = mapConfig;
  });

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

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