多个angularjs指令实例,控制器作为语法

时间:2015-02-09 15:53:05

标签: javascript angularjs controller directive

我使用带有controllerAs语法的指令。我想使用该指令的2个实例,它们都需要非共享数据。我的问题,似乎只有控制器实例。

似乎可以选择在指令声明中添加范围:{}。但后来我会被迫使用基于范围的访问?

或者还有其他方法仍然使用controllerAs / this语法并强制使用angular来创建2个不同的控制器对象吗?

app.directive('createA', function() {
    return {
        restrict : 'E',
        templateUrl : '/template/createActivity',

        // using this to create 2 distinct scopes 
        // but then I can't use the this/caCtrl.data syntax?
        scope      : {},

        controller : function($scope, $rootScope) {

            // using this syntax to access data 
            // in tempate like {{ caCtrl.data }}
            this.data = '123';
        },
        controllerAs : 'caCtrl'

    };
});

1 个答案:

答案 0 :(得分:3)

我相信你要找的是指令的bindToController属性。查看此博文http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html

相关问题