繁体   English   中英

AngularJS模板组件未呈现变量

[英]AngularJS template component not rendering variables

我的目标是使用angularjs组件绘制条形图angularJs图表。 因此,如果数据发生更改,我需要使用主控制器的绑定。

我有一个angularjs控制器,带有一个名为“ labels”的数组,如下所示:

var app = angular.module("app", ["ui.router", "ngCsv" ,'ngSanitize','ui.bootstrap' ])

app.controller('AnalyseController',AnalyseController)

AnalyseController.$inject = ['$scope'];

function AnalyseController($rootScope, $scope, $timeout, $http, $location, $window, $filter, $mdToast, $mdDialog, $sce, $state) {

 labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
}

然后,我试图从这样的angularjs组件访问“标签”:

app.component('graphique', {
    templateUrl:'_commerciaux/views/include/graphique.html',
    bindings:{labels:'='},
    controller : [ '$http','$scope','$rootScope','toastr',function control($http,$scope,$rootScope,toastr){

        console.log(labels);

        this.labels = labels;
        console.log(this.labels);

         this.$onInit = function() {            
              this.labels = labels;
            };

   }]
}) 

嗯,控制台日志很好地显示了标签,但是没有办法在模板内显示此变量:它似乎是undefined。 奇怪的是我在控制台内看到了它。

在此处输入图片说明

我正在尝试生成一个图,它也不起作用,但是一旦AI不使用“绑定过程”并直接在组件内部设置变量,它就会起作用!

这是我的html模板,在父控制器中设置了$ ctrl.labels却没有显示,并且该组件应该以2种方式绑定它:

<div class="chart-wrapper analyse_graph"   >
    <canvas class="chart chart-line" chart-data="$ctrl.data" chart-labels="$ctrl.labels" chart-legend="true" chart-series="$ctrl.series" chart-options="$ctrl.options" chart-colors="$ctrl.colors" height="70"></canvas>    
</div>
<div>

     {{$ctrl.labels}} // NOT SHOWING !!

</div>

测试1:我已经这样更改了控制器:

  function AnalyseController($rootScope, $scope, $timeout, $http, $location, $window, $filter, $mdToast, $mdDialog, $sce, $state) {

     $scope.labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
    }

但是现在,控制台显示: ReferenceError:“未定义标签” ,(此消息来自组件)。 因此,只要我不使用$ scope,它就可以在控制台内运行,但是模板不会呈现绑定变量...

编辑2:这就是我所说的模板,非常简单:

<graphique ></graphique>

编辑3:只要让您知道图的工作原理,只要我直接在组件内部设置变量,然后删除绑定部分即可:

HTML内的组件调用:

<graphique ></graphique>

组件本身:

app.component('graphique', {
    templateUrl:'_commerciaux/views/include/graphique.html',
    controller : [ '$http','$scope','$rootScope','toastr',function control($http,$scope,$rootScope,toastr){


        this.labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
        this.data = [[12,24,40,35,20,12,55,78,45,12,32,45],[10,15,30,35,12,15,70,24,25,10,47,25]];
}

模板:

<div class="chart-wrapper analyse_graph"   >
    <canvas class="chart chart-line" chart-data="$ctrl.data" chart-labels="$ctrl.labels" chart-legend="true" chart-series="$ctrl.series" chart-options="$ctrl.options" chart-colors="$ctrl.colors" height="70"></canvas>    
</div>
<div>

{{$ctrl.labels}}

</div>

注意:是的,{{$ ctrl.labels}}可以很好地显示,但是只要我尝试从组件传递带有“绑定”部分的标签,它就不再起作用,模板也不会显示。

不幸的是,如果我不能解决这个问题,我必须使用老式的$ emit并复制大量图表和老式控制器,因为“绑定”无效。 也许是因为我在主控制器中使用的$ scope语法:可能太旧了..

解决的嘿! 我使用的是angularjs 1.68,所以我应该在以前看过它:

https://code.angularjs.org/1.6.10/docs/guide/component

它与最新版本有所不同!

所以这现在是我的主要观点(请注意缺少的“ as ctrl”):

<div ng-controller="AnalyseController  as ctrl" style="overflow:auto;max-height:1000px;">

接下来,在我的主控制器中,我现在设置如下变量:

this.labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
this.data = [[12,24,40,35,20,12,55,78,45,12,32,45],[10,15,30,35,12,15,70,24,25,10,47,25]];

然后我的组件现在看起来像这样:

app.component('graphique', {
    templateUrl:'_commerciaux/views/include/graphique.html',
    bindings:{labels:'=',data:'='},
    controller : [ '$http','$scope','$rootScope','toastr',function control($http,$scope,$rootScope,toastr){

 // No vars or init there
})

最后,这是我视图内的组件调用,请注意变量调用:

<graphique labels="ctrl.labels" data="ctrl.data"></graphique>

最后,我的模板仍然引用$ ctrl vars:

<div class="chart-wrapper analyse_graph"   > 
    <canvas class="chart chart-line" chart-data="$ctrl.data" chart-labels="$ctrl.labels" chart-legend="true" chart-series="$ctrl.series" chart-options="$ctrl.options" chart-colors="$ctrl.colors" height="70"></canvas>     
 </div>
 <div> 

{{$ctrl.labels}}

</div>

非常感谢您的帮助! Angularjs是超级TOP,我的projet版本是1.6.8,这就是为什么谈论组件时它有所不同的原因!

mygraph

如果使用$ ctrl语法:

function AnalyseController($rootScope, $scope, $timeout, $http, $location, $window, $filter, $mdToast, $mdDialog, $sce, $state) {
    var ctrl = this;

    ctrl.labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
}

对于模板,您需要传递参数

<graphique labels="$ctrl.labels"></graphique>

如果使用$ scope语法:

function AnalyseController($rootScope, $scope, $timeout, $http, $location, $window, $filter, $mdToast, $mdDialog, $sce, $state) {
    $scope.labels = ['Toutes','Carrefour','Intermarché','Leclerc','Super U ','Carrefour','Lidl','Intersport','Decathlon','Dia','Renault','Osiatis'];
}

并在模板中

<graphique labels="labels"></graphique>

暂无
暂无

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

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