angularjs - Angular+TypeScript - görünümden denetleyiciye parametre geçirme


AngularJS'i TypeScript ile kullanıyorum ve şimdi bir parametreyi view'dan controller'a iletmem gerekiyor. Bunu yapmaya çalıştığım yol şöyle ( ng-init aracılığıyla):

<div class="col-md-9" ng-controller="MapIZSController" ng-init="init('IZS')">

Kontrolöre "IZS" değeri gönderilmelidir. Kontrolör şu şekilde görünmektedir:

export class MapIZSController
{
    static $inject = ["$scope", "leafletData"];
    private m_scope: IMapIZSScope;
    private m_leafletData;

    constructor($scope: IMapIZSScope, leafletData)
    {
        // the first way I tried
        $scope.init = function (type) {
            console.log("type is: " + type);
        };
        // the second way I tried
        $scope.init = this.init;
    }

    public init = (init: any) => {
        console.log("init is: " + init)
    } 

Benim sorunum, bir tür elde etmek istemem ama

  • ilk yol asla çağrılmaz ve
  • ikinci de.

Bana biraz tavsiye verebilir misin lütfen?

Cevap #1

Söylerdim ki doğru yoldasınız. Çalışan bir plunker var.

Bu biraz sadeleştirilmiş kontrolör olabilir:

namespace MyNamespace 
{   
  export class MapIZSController
  {
    static $inject = ["$scope", "leafletData"];
    private m_scope: IMapIZSScope;
    private m_leafletData;

    private _type;
    constructor($scope: IMapIZSScope, leafletData)
    {
        // // the first way I tried
        // $scope.init = function (type) {
        //     console.log("type is: " + type);
        // };
        // // the second way I tried
        // $scope.init = this.init;
    }

    public init = (type: any) => {
        this._type = type
        console.log("type is: " + this._type)
    } 
  }
}

ve bu şekilde onu başlatabiliriz:

<div class="col-md-9" 
  ng-controller="MapIZSController as ctrl" 
  ng-init="ctrl.init('IZS')">
</div>

Yani, controllerAs yaklaşımını kullanıyoruz ve kontrolöre ctrl. ile erişebiliyoruz ... ve bu şekilde ctrl.init() çağırıyoruz

Buradan kontrol edin

İlgili makaleler