指令-AngularJS中的控制器数据绑定

指令-AngularJS中的控制器数据绑定,angularjs,Angularjs,我已经为此挣扎了好几个小时了 var testApp = angular.module('testApp', []); testApp.directive('test', function() { return { restrict: 'E', transclude: true, template: '<div ng-transclude>Hello World</div>', link: func

我已经为此挣扎了好几个小时了

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

testApp.directive('test', function() {
    return {
        restrict: 'E',
        transclude: true,
        template: '<div ng-transclude>Hello World</div>',
        link: function(scope) {

        }
    }
});

testApp.controller('testCtrl', function ($scope) {
    $scope.user = "";
});
var testApp=angular.module('testApp',[]);
testApp.directive('test',function(){
返回{
限制:'E',
是的,
模板:“你好,世界”,
链接:功能(范围){
}
}
});
testApp.controller('testCtrl',函数($scope){
$scope.user=“”;
});
下面是JSFIDLE:

现在,我只需要在指令中嵌入一个输入,以便能够在testCtrl控制器中直接反映用户模型。
我对这个beast是如何工作的感到困惑,因为我教过在这种情况下作用域是共享的,不是吗?

ngTransclude
创建了一个新的子作用域,该子作用域通常继承自它的父作用域

在子范围上使用基元时,它会对父范围的变量进行阴影处理

已经说过一千遍了:使用点符号

控制器:

testApp.controller('testCtrl', function ($scope) {
    $scope.data = { user : "Hello World" };
});
<input type="text" ng-model="data.user"/><br />
Directive model:<span>{{ data.user }}</span>  
html:

testApp.controller('testCtrl', function ($scope) {
    $scope.data = { user : "Hello World" };
});
<input type="text" ng-model="data.user"/><br />
Directive model:<span>{{ data.user }}</span>  

指令模型:{{data.user}
查看我的其他答案以了解描述: