2016-02-10 78 views
0

如果您會看到代碼,您會注意到我將template屬性設置爲{{$.aaa}}。有沒有辦法避免在變量前使用controllerAs前綴,並使用表格{{aaa}}的模板?使用不帶「controllerAs」前綴的模板

(function(){ 
    "use strict"; 

    class Controller { 
     constructor(){ 
      this.aaa = 111; 
     } 

     doSomething(){ 
      alert(this.aaa) 
     } 
    } 

    var app = angular.module('app', []); 

    app.directive('any', function() { 
     return { 
      restrict: 'E', 
      controller: Controller, 
      controllerAs: '$', 
      template: '{{$.aaa}}' 
     }; 
    }); 
})(); 
+2

應該是$範圍的財產不屬於控制器。請注意,現在控制器的語法是首選方式。 –

回答

0

將$ scope注入到您的控制器構造函數中,然後將您的屬性附加到作用域。

class Controller { 
    static $inject = ["$scope"]; 

    constructor($scope) { 
     $scope.aaa = "value"; 
    } 
} 

和:

app.directive('any', function() { 
    return { 
     restrict: 'E', 
     controller: Controller, 
     template: '{{aaa}}' 
    }; 
}); 
在這種情況下, 'AAA'