Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 通过$compile将scope对象传递给Angular指令_Angularjs_Directive - Fatal编程技术网

Angularjs 通过$compile将scope对象传递给Angular指令

Angularjs 通过$compile将scope对象传递给Angular指令,angularjs,directive,Angularjs,Directive,我知道如果我有HTML指令,我可以将对象从$scope传递给它: <mydirective some-data="someData"></mydirective> 是的,您可以将对象传递给 var myApp=angular.module('myApp',[]); myApp.directive('passObject',function()){ 返回{ 限制:'E', 作用域:{obj:'='}, 模板:“你好,{{obj.prop}}!” }; }); myApp

我知道如果我有HTML指令,我可以将对象从$scope传递给它:

<mydirective some-data="someData"></mydirective>

是的,您可以将对象传递给

var myApp=angular.module('myApp',[]);
myApp.directive('passObject',function()){
返回{
限制:'E',
作用域:{obj:'='},
模板:“你好,{{obj.prop}}!”
};
});
myApp.controller('MyCtrl',函数($scope){
$scope.obj={prop:“world”};
});

是,您可以将对象传递给

var myApp=angular.module('myApp',[]);
myApp.directive('passObject',function()){
返回{
限制:'E',
作用域:{obj:'='},
模板:“你好,{{obj.prop}}!”
};
});
myApp.controller('MyCtrl',函数($scope){
$scope.obj={prop:“world”};
});


您是否尝试过
$compile(“”)?(您刚才在you
$scope
中设置的var的名称)@jlowcs omg,这非常简单,现在也非常明显。非常感谢。@jlowcs您应该将此作为一个答案添加进来,这样会更容易、更清楚地找到我您是否尝试过
$compile(“”)?(您刚才在you
$scope
中设置的var的名称)@jlowcs omg,这非常简单,现在也非常明显。非常感谢。@jlowcs您应该添加此作为答案,这样更容易、更清楚地找到它
$scope.someData = { firstName: 'John', lastName: 'Doe' };

var link = $compile("<mydirective some-data='???'></mydirective>");
var content = link($scope);
$(body).append(content);