AngularJS - 没有调用函数?

时间:2017-02-07 08:12:25

标签: javascript angularjs controller

我在控制器中写下了以下代码

var chart = c3.generate({
    data: {
         columns: [
              ['data1', 30543, 2045346, 50765767, 4067657, 605676, 50665],
              ['data2', 200, 130, 90, 240, 130, 220]
         ],
         type: 'bar',
         types: {
              data2: 'line'
         }
     },
     axis: {
         y2: {
              show: true
             }
     }
});

我试图这样调用,但点击按钮function SteppersDemoCtrl($mdStepper, $timeout) { alert('hi'); this.$mdStepper = $mdStepper; this.$timeout = $timeout; this.isVertical = true; this.isLinear = false; this.isAlternative = true; this.isMobileStepText = true; this.campaign = false; } SteppersDemoCtrl.prototype.previousStep = function () { alert("Hariom"); var steppers = this.$mdStepper('stepper-demo'); steppers.back(); }; SteppersDemoCtrl.prototype.cancel = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.back(); }; SteppersDemoCtrl.prototype.nextStep = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.next(); }; SteppersDemoCtrl.prototype.toggleMobileStepText = function () { this.isMobileStepText = !this.isMobileStepText; }; SteppersDemoCtrl.prototype.toggleLinear = function () { this.isLinear = !this.isLinear; }; SteppersDemoCtrl.prototype.toggleAlternative = function () { this.isAlternative = !this.isAlternative; }; SteppersDemoCtrl.prototype.toggleVertical = function () { this.isVertical = !this.isVertical; }; SteppersDemoCtrl.prototype.showError = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.error('Wrong campaign'); }; SteppersDemoCtrl.prototype.clearError = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.clearError(); }; SteppersDemoCtrl.prototype.showFeedback = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.showFeedback('Step 1 looks great! Step 2 is comming up.'); }; SteppersDemoCtrl.prototype.clearFeedback = function () { var steppers = this.$mdStepper('stepper-demo'); steppers.clearFeedback(); }; SteppersDemoCtrl.$inject = [ '$mdStepper', '$timeout' ]; sureApp.controller('SteppersDemoCtrl', ['$scope', '$http', '$location', '$log', '$interval', 'uiGridConstants', 'sureHttpService', 'sureCache' , function ($scope, $http, $location, $log, $interval, uiGridConstants, sureHttpService, sureCache) { $scope.templates = []; $scope.selectedTemplate = {}; $scope.fetchPathTemplates = function () { var restUrl = "SomeURL" sureHttpService.sureAjaxService(restUrl, false, false, false).then(function (response) { if (response && response.collection && response.collection.element) { $scope.templates = response.collection.element; console.log("rssssssssss >>>"+$scope.templates); } else { // column attrubute response data error } }, function (error) { alert("error code " + error) }); } }]); 不会调用,日志中没有错误。我可以致电selectCampaign()

fetchPathTemplates()

1 个答案:

答案 0 :(得分:0)

您应该将方法定义为$scope的属性,以便从视图中调用它们,不需要将它们添加到控制器的prototype

控制器:

angular.module('myapp').controller('myCtrl', myCtrl);

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

function myCtrl($scope) {
    $scope.myMethod = function() {
        console.log('this works');
    }
}

查看代码:

<div ng-controller="myCtrl as vm">
    <button ng-click="vm.myMethod();">Test</button>
</div>