【问题标题】:Set variable in controller that can be seen by directive在控制器中设置指令可以看到的变量
【发布时间】:2015-10-13 00:10:26
【问题描述】:

我想将一些数据从控制器传递到指令,所以先指令:

.directive('myDirective', function() {
  'use strict';

  return {
    restrict: 'EA', 
    // templateUrl here
    controller: DataController,

    link: function(scope, elem, attrs, ctrl) {

        var data = ctrl.data,

        config = {
          data: data,
          xkey: 'y',
          ykeys: ['a', 'b'],
          // ... more code here
        };
      //.. more  irrelevant code
   }
};

当我这样放置数据时,效果很好:

nslive.controller('DataController', ['Data', 'socketio', '$routeParams', DataController]);

function DataController(Data, socketio, $routeParams) {
  'use strict';

  var vm = this;

  vm.data = [
    {y: '2014', a: 12500, b: 38000}, {y: '2015', a: 10500, b: 27000},
    {y: '2016', a: 38640, b: 13545}, {y: '2017', a: 38640, b: 13000}
  ];

  // more code here
}

但是,当我像这样将它放在 '.success()' 回调函数中时,指令无法看到数据:

nslive.controller('DataController', ['Data', 'socketio', '$routeParams', DataController]);

function DataController(Data, socketio, $routeParams) {
  'use strict';

  var vm = this;
  vm.urlJobname = $routeParams.jobname;

  Data
    .all(vm.urlJobname)
    .success(function(data) {
      console.log('I got here!'); // this is shown successfully in the chrome console
      vm.data = [
        {y: '2014', a: 12500, b: 38000}, {y: '2015', a: 10500, b: 27000},
        {y: '2016', a: 38640, b: 13545}, {y: '2017', a: 38640, b: 13000}
      ];
    });
}

我觉得和变量作用域有关,但是不知道怎么设置这样的全局变量,请指教。谢谢。

【问题讨论】:

  • 您可以将$scope 传递给指令。或在指令中使用scope: {<some variables>} 并在任何controller 中填充特定变量

标签: angularjs variables controller scope directive


【解决方案1】:

这是因为在promise/request返回成功消息之前编译了指令并执行了ink函数,, 所以它将被编译,我建议将你的逻辑包装在 $watch 中,并在第一次执行后清除该手表

所以这将是你的链接功能

function(scope, elem, attrs, ctrl){
   scope.clearWatch = scope.$watch(function(){return ctrl.data;},function(newVal){
       if(newVal){
          //your logic goes here ,, we are sure that ctrl.data has value here
         // here we call clearWatch to stop watching on that value
         scope.clearWatch();
       }
   });
}

【讨论】:

  • 确实有效。非常感谢。顺便说一句,你错过了括号,哈哈@Ahmed Eid
  • 得到一个关于$watch函数的问题,为什么第一个参数应该是一个函数(返回ctrl.data)而不是'ctrl.data'直接?@Ahmed Eid
  • 哈哈 :D :D 我修好了 :) 关于 watchExpression ,点击这个链接:docs.angularjs.org/api/ng/type/$rootScope.Scope ,然后搜索“watchExpression”,它只接受字符串和函数
【解决方案2】:

在指令中:

scope: {
          object: '='
       }

最佳做法是为指令使用Controller,而不是将其放入link 函数中。

在控制器指令中:

$scope.$watch('object', function (){
      // do something with data
});

在 HTML 中:

<my-directive object=objectFromController></my-directive>

在控制器中:

$scope.objectFromController = data;

【讨论】:

  • 是的,有道理。让我试试。@maddog
  • 问题,在控制器指令中添加$watch func,什么是控制器指令,是.directive定义中的控制器吗?或在我单独的控制器文件中?谢谢。 @maddog
  • 你可以在指令中定义controller。然后你可以在单独的文件或同一个文件中定义。可以参考:weblogs.asp.net/dwahlin/…
  • 是的,完成了,它也能正常工作。非常感谢,我学会了。@maddog
猜你喜欢
  • 1970-01-01
  • 2013-06-16
  • 1970-01-01
  • 2016-01-11
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
相关资源
最近更新 更多