ng-controller和controller属性之间的区别

时间:2021-10-26 13:36:51

I have been trying to figure this out for a while, but I can't find anywhere that describes it and my testing has not given me any answers.

我一直试图弄清楚这一点,但我找不到描述它的任何地方,我的测试没有给我任何答案。

I have created a custom directive and I want the directive's scope to be a child-scope of the parent scope. I've found two different approaches that solves this.

我创建了一个自定义指令,我希望指令的范围是父范围的子范围。我找到了两种解决这个问题的方法。

A:

A:

angular.module('myModule').directive('myDirective', function(){
    return{
        scope: true,
        template: '<div>{{var}}</div>',
        controller: function($scope){
            $scope.var = 123;
        }
    };
});

B:

B:

angular.module('myModule').directive('myDirective', function(){
    return{
        scope: false,
        template: '<div ng-controller="MyController">{{var}}</div>'
    };
});

angular.module('myModule').controller('MyController', function($scope){
    $scope.var = 123;
});

What are the differences between these two approaches? Both seem to do what I want, but are there some advantages/disadvantages of using one approach over the other?

这两种方法有什么区别?两者似乎都做我想要的,但是使用一种方法比另一种方法有一些优点/缺点吗?

Any guidance is appreciated.

任何指导表示赞赏。

3 个解决方案

#1


2  

You shouldn't use the latter unless you have a really good reason to do so. If you want to create a reusable controller and use that in your directive, just define the directive as such:

除非你有充分的理由这样做,否则你不应该使用后者。如果要创建可重用的控制器并在指令中使用它,只需定义指令:

angular.module('myModule').directive('myDirective', function(){
    return{
        scope: false,
        controller: 'MyController',
        template: '{{var}}'
    };
});

#2


1  

The second approach would allow you to reuse the controller on several elements. The first one is distinct for that directiv and could be shared.

第二种方法允许您在几个元素上重用控制器。第一个是明确的,可以分享。

#3


1  

Option B seems able to share the controller. However, from the design perspective, the controllers should not be shared. A controller should contain some particular business logic.

选项B似乎能够共享控制器。但是,从设计角度来看,不应共享控制器。控制器应包含一些特定的业务逻辑。

If some model/operations are to be shared, extract to services.

如果要共享某些模型/操作,请提取到服务。

#1


2  

You shouldn't use the latter unless you have a really good reason to do so. If you want to create a reusable controller and use that in your directive, just define the directive as such:

除非你有充分的理由这样做,否则你不应该使用后者。如果要创建可重用的控制器并在指令中使用它,只需定义指令:

angular.module('myModule').directive('myDirective', function(){
    return{
        scope: false,
        controller: 'MyController',
        template: '{{var}}'
    };
});

#2


1  

The second approach would allow you to reuse the controller on several elements. The first one is distinct for that directiv and could be shared.

第二种方法允许您在几个元素上重用控制器。第一个是明确的,可以分享。

#3


1  

Option B seems able to share the controller. However, from the design perspective, the controllers should not be shared. A controller should contain some particular business logic.

选项B似乎能够共享控制器。但是,从设计角度来看,不应共享控制器。控制器应包含一些特定的业务逻辑。

If some model/operations are to be shared, extract to services.

如果要共享某些模型/操作,请提取到服务。