AngularJS:如何设置控制器&x27;当使用;控制器为;语法?

AngularJS:如何设置控制器&x27;当使用;控制器为;语法?,angularjs,angularjs-directive,angularjs-controller,Angularjs,Angularjs Directive,Angularjs Controller,考虑以下使用的示例: {{my.message}} 点击我 var myApp=angular.module('myApp',[]); myApp.controller('MyCtrl',function(){ this.message='Hello'; }); myApp.directive('myDirective',function(){ 返回{ 限制:“A”, 链接:函数(范围、元素、属性){ 元素绑定('单击',函数(){ //在此处更改“消息” }); } }; }); 如何从

考虑以下使用的示例:


{{my.message}}
点击我
var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl',function(){
this.message='Hello';
});
myApp.directive('myDirective',function(){
返回{
限制:“A”,
链接:函数(范围、元素、属性){
元素绑定('单击',函数(){
//在此处更改“消息”
});
}
};
});
如何从指令中设置控制器的
消息

如果没有“Controller As”语法,我只需执行以下操作:

scope.message='World';
作用域:$apply();

但是,当使用“Controller As”语法时,您将如何执行此操作?

因为控制器定义为my,您需要使用:

scope.my.message = 'World';
scope.$apply();

由于控制器定义为“我的”,因此您需要使用:

scope.my.message = 'World';
scope.$apply();