Angularjs 嵌套指令中无法识别控制器的作用域变量

Angularjs 嵌套指令中无法识别控制器的作用域变量,angularjs,Angularjs,我创建了两个指令,并将第一个指令插入到第二个指令中。模板属性的内容工作正常,但无法识别控制器的范围变量。请给我提供这个问题的解决方案 示例链接:您没有为第二个指令提供属性 HTML <div second-dir first-dir-scope="content"> <div first-dir first-dir-scope="content"></div> </div> 链接演示:最好的选择是使用父指令,我们可以使用指令的requ

我创建了两个指令,并将第一个指令插入到第二个指令中。模板属性的内容工作正常,但无法识别控制器的范围变量。请给我提供这个问题的解决方案


示例链接:

您没有为第二个指令提供属性

HTML

<div second-dir first-dir-scope="content">

  <div first-dir first-dir-scope="content"></div>

</div>


链接演示:

最好的选择是使用父指令,我们可以使用指令的
require
选项,如
require:“?secondDir”
中的
firstDir

代码

var myApp = angular.module("myApp", []);

myApp.controller("myController", function($scope) {
    $scope.content = "test1";
});

myApp.directive("firstDir", function() {
    return {
        restrict: "AE",
        require: '?secondDir',
        scope: {
            firstDirScope: "="
        },
        template: "<div>first content</div>",
        link: function(scope, element, attrs, secondDir) {
            console.log(scope.firstDirScope, 'first');
        }
    };
});

myApp.directive("secondDir", function() {
    return {
        restrict: "AE",
        scope: {
            firstDirScope: "="
        },
        controller: function($scope) {
            console.log($scope.firstDirScope, 'second');
        }
    };
});
var myApp=angular.module(“myApp”,[]);
控制器(“myController”,函数($scope){
$scope.content=“test1”;
});
myApp.directive(“firstDir”,function()){
返回{
限制:“AE”,
require:“?secondDir”,
范围:{
firstDirScope:“”
},
模板:“第一个内容”,
链接:函数(范围、元素、属性、secondDir){
log(scope.firstDirScope,'first');
}
};
});
myApp.directive(“secondDir”,function()){
返回{
限制:“AE”,
范围:{
firstDirScope:“”
},
控制器:功能($scope){
log($scope.firstDirScope,'second');
}
};
});

非常感谢。在js文件中是否有其他方法可以执行而不是查看您可以使用“console.log(scope.parent.content);”获取内容值。但这是个坏主意!我们可以在第二个指令中使用require“^firstDir”吗?@user1153484是的,您可以这样做,这样做会更好