kingsman kingsman - 19 days ago 7
AngularJS Question

Angularjs Multiple Controllers, only the first one works

I'm using angularjs 1.5.8 to basically mimic an excel table with equations. THere are three rows requiring two inputs that affect two outputs. I'm struggling on how to write my script to include multiple controllers as I'm building a controller for each row. If I comment out the second and third controller in my script, the first row works just fine. When no lines are commented, nothing works. I feel like I'm very close but not sure where I'm going wrong. Any help would be appreciated.

Here is the working controller:

angular.module('ADMApp', [])
.controller('ADMFarViewController', [function() {
var ctrl = this;
ctrl.ADMCalc = function() {
var ADMFarViewImgHeightvar = Number(ctrl.ADMFarViewImgHeightvar || 0);
var ADMFarViewVertPixvar = Number(ctrl.ADMFarViewVertPixvar || 0);
ctrl.ADM_FarViewFarthestViewer_ans = (ADMFarViewImgHeightvar * 3438) / ADMFarViewVertPixvar;
ctrl.ADM_FarViewViewRat_ans = ctrl.ADM_FarViewFarthestViewer_ans / ADMFarViewImgHeightvar;
}
}]);


Here is my plunker: https://plnkr.co/edit/6nKEPCeOqx7zEFx5M5WZ

Answer

Don't redefine your module every time.

// Code goes here
var ADMApp = angular.module('ADMApp', []);

ADMApp.controller('ADMFarViewController', [function() {
    var ctrl = this;
    ctrl.ADMCalc = function() {
      var ADMFarViewImgHeightvar = Number(ctrl.ADMFarViewImgHeightvar || 0);
      var ADMFarViewVertPixvar = Number(ctrl.ADMFarViewVertPixvar || 0);
      ctrl.ADM_FarViewFarthestViewer_ans = (ADMFarViewImgHeightvar * 3438) / ADMFarViewVertPixvar;
      ctrl.ADM_FarViewViewRat_ans = ctrl.ADM_FarViewFarthestViewer_ans / ADMFarViewImgHeightvar;
    }
}]);



ADMApp.controller('ADMMinImgController', [function() {
        var ctrl = this;
        ctrl.ADMCalc = function() {
            var ADM_MinImgHeightVertPix_var = Number(ctrl.ADM_MinImgHeightVertPix_var || 0);
            var ADM_MinImgHeightFarthestViewer_var = Number(ctrl.ADM_MinImgHeightFarthestViewer_var || 0);
            ctrl.ADM_MinImgHeightImageHeight_ans = (ADM_MinImgHeightVertPix_var * ADM_MinImgHeightFarthestViewer_var) / 3438;
            ctrl.ADM_MinImgHeightViewRat_ans = ADM_MinImgHeightFarthestViewer_var / ctrl.ADM_MinImgHeightImageHeight_ans;
        }
}]);


ADMApp.controller('ADMMaxImgController', [function() {
        var ctrl = this;
        ctrl.ADMCalc = function() {
            var ADM_MaxImgImageHeight_var = Number(ctrl.ADM_MaxImgImageHeight_var || 0);
            var ADM_MaxImgFarthestViewer_var = Number(ctrl.ADM_MaxImgFarthestViewer_var || 0);
            ctrl.ADM_MaxImgVertPix_ans = (ADM_MaxImgImageHeight_var * 3438) / ADM_MaxImgFarthestViewer_var;
            ctrl.ADM_MaxImgViewRat_ans = ADM_MaxImgFarthestViewer_var / ADM_MaxImgImageHeight_var;
        }
}]);
Comments