来自Controller的访问指令方法

时间:2017-11-07 09:35:35

标签: angularjs typescript

我正在使用Typescript在AngularJs中编写应用程序,但我无法从控制器类访问指令中的方法。我将'control:'='添加到指令的范围内,认为这会将指令中方法的控制绑定到控制器,但是我收到错误

控制器代码为:

import {DaypartingSegmentsContainer} from "lib/js/publisher/deal/objects/dayparting/dayparting.segments.container";


@Component({
    bindings: {
        deal: "="
    },
    controllerAs: "$ctrl",
    module: DealApp,
    require: {
        DealForm: '^form',
    },
    selector: 'deal-edit-dayparting',
    template: require('./templates/deal.edit.dayparting.html'),
})
@Inject(
    "$scope",
    "$q"

)
export class DealEditDaypartingCtrl extends SxControllerBase {

    public daypartingSegmentsContainer : DaypartingSegmentsContainer;

    constructor(protected $scope : ng.IScope,
                private $q : angular.IQService) {
        super($scope);
    }

    public $onInit() : void {
        let self : DealEditDaypartingCtrl = this;

        this.daypartingSegmentsContainer.getCommonTimes();

    }
}

指令类是:

import {Inject, Directive} from "lib/js/angularjs/decorators/sx-forward";
import DealApp from "lib/js/publisher/deal/lib.deal.app";
import DealEditDaypartingCtrl from "lib/js/publisher/deal/edit/deal.edit.dayparting";
import "lib/js/publisher/deal/objects/dayparting/dayparting.jquery.factory";
import {DaypartingDayName} from "lib/js/publisher/deal/objects/dayparting/dayparting.day.name";

import {
    minutesToString,
    stringToMinutes
} from "lib/js/publisher/deal/objects/dayparting/dayparting.time.functions";

@Directive({
    selector: 'dayparting-segments-container',
    require: '^dealEditDayparting',
    replace: true,
    module: DealApp,
    scope: {
        dayName: '<',
        control: '=',
    },
    template: require('./templates/dayparting.segments.container.html'),
    controllerAs: '$ctrl',
    link: function(scope : any, element : any, attrs : any, daypartingCtrl : DealEditDaypartingCtrl) : void {
        scope.daypartingCtrl = daypartingCtrl;

        // Delete button watch
        scope.$watch(
            () => {
                return daypartingCtrl.deleteSegment;
            },
            (shouldDelete : boolean) => {
                if (shouldDelete === true) {
                    scope.$ctrl.onDelete();
                }
            }
        );

        // Calendar leave watch
        scope.$watch(
            () => {
                return daypartingCtrl.calendarLeave;
            },
            (calendarLeave : boolean) => {
                if (calendarLeave === true) {
                    scope.$ctrl.onCalendarMouseLeave();
                }
            }
        );
    },
})

@Inject('$scope', 'JQueryFactory' )

export class DaypartingSegmentsContainer {
  
    constructor(private $scope : any,
                private JQueryFactory : any) {
        this.tooltipIsOpen = false;
        this.jquery = JQueryFactory.get();
    }

    public getCommonTimes() : void {

        console.log("i am in getCommon times!!!")

    }
}


export default DaypartingSegmentsContainer;

我不断收到控制台错误 - Cannot read property 'getCommonTimes' of undefined

1 个答案:

答案 0 :(得分:1)

代码在全职盯着我看。我添加了以下代码,现在正在使用:

mail.smtp.user=usermail@gmail.com
mail.smtp.password=userpassword
mail.smtp.host=smtp.gmail.com
mail.smtp.port=465
mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.debug=true
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false