Angularjs 访问元素指令子项

Angularjs 访问元素指令子项,angularjs,Angularjs,我正在执行一个导航指令,该指令应为每个导航项目提供a元素: <navigation title="My Web Page"> <a href="#">Home</a> <a href="#">About</a> </navigation> 我错过了什么?我期待着在指令范围内附加一个锚数组,并在模板中通过它们进行迭代。我有点困惑您的导航TEM来自何处,但我会尝试一下 我假设您的导航元素是在指令的父控制器中定义的 fu

我正在执行一个
导航
指令,该指令应为每个导航项目提供
a
元素:

<navigation title="My Web Page">
  <a href="#">Home</a>
  <a href="#">About</a>
</navigation>

我错过了什么?我期待着在指令范围内附加一个锚数组,并在模板中通过它们进行迭代。

我有点困惑您的导航TEM来自何处,但我会尝试一下

我假设您的导航元素是在指令的父控制器中定义的

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
您必须在指令中将数组声明为属性

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });

并将其双向绑定到指令的范围

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
.directive('navigation',function(){
返回{
模板:“”,
限制:'E',
替换为:“true”,
范围:{
导航:'='
},
链接:函数postLink(范围、元素、属性){
}
};
});
请记住,您希望在链接函数中远离DOM操作。相反,我建议在模板中使用ng repeat,并确保nav项的数组被传递到指令的作用域


编辑:看小提琴

我有点困惑你的导航技术是从哪里来的,但我会试试看

我假设您的导航元素是在指令的父控制器中定义的

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
您必须在指令中将数组声明为属性

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });

并将其双向绑定到指令的范围

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
.directive('navigation',function(){
返回{
模板:“”,
限制:'E',
替换为:“true”,
范围:{
导航:'='
},
链接:函数postLink(范围、元素、属性){
}
};
});
请记住,您希望在链接函数中远离DOM操作。相反,我建议在模板中使用ng repeat,并确保nav项的数组被传递到指令的作用域


编辑:看小提琴

我有点困惑你的导航技术是从哪里来的,但我会试试看

我假设您的导航元素是在指令的父控制器中定义的

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
您必须在指令中将数组声明为属性

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });

并将其双向绑定到指令的范围

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
.directive('navigation',function(){
返回{
模板:“”,
限制:'E',
替换为:“true”,
范围:{
导航:'='
},
链接:函数postLink(范围、元素、属性){
}
};
});
请记住,您希望在链接函数中远离DOM操作。相反,我建议在模板中使用ng repeat,并确保nav项的数组被传递到指令的作用域


编辑:看小提琴

我有点困惑你的导航技术是从哪里来的,但我会试试看

我假设您的导航元素是在指令的父控制器中定义的

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
您必须在指令中将数组声明为属性

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });

并将其双向绑定到指令的范围

function myCtrl ($scope){
    $scope.navArray=[{title: 'Link1', href: 'www.example.com'}, {...}];
}
<navigation nav="navArray"></navigation>
.directive('navigation', function () {
    return {
      template: '<div><a ng-repeat="link in nav" href="link.href">{{link.title}}</a></div>',
      restrict: 'E',
      replace: 'true',
      scope: {
        nav: '='
      },
      link: function postLink(scope, element, attrs) {

      }
    };
  });
.directive('navigation',function(){
返回{
模板:“”,
限制:'E',
替换为:“true”,
范围:{
导航:'='
},
链接:函数postLink(范围、元素、属性){
}
};
});
请记住,您希望在链接函数中远离DOM操作。相反,我建议在模板中使用ng repeat,并确保nav项的数组被传递到指令的作用域


编辑:请参见Fiddle

要在新模板中移动原始内容,您需要使用
transclude
属性。当translude设置为true时,该指令将删除原始内容,但也可通过
ng translude
指令将其重新插入模板中。见下面的例子

如果不半透明原始数据,锚定标记将被删除,这就是链接函数无法找到它们的原因

.directive('navigation', function () {
    return {
        template: '<div>Tansclude data here: <span ng-translude></span></div>',
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            title: '@'
        },
        link: function postLink(scope, element, attrs) {
            console.log($(element).find('a'));            
        }
   };
});
.directive('navigation',function(){
返回{
模板:“此处的Tansclude数据:”,
限制:'E',
替换:正确,
是的,
范围:{
标题:“@”
},
链接:函数postLink(范围、元素、属性){
log($(element.find('a'));
}
};
});

要在新模板中移动原始内容,需要使用
transclude
属性。当translude设置为true时,该指令将删除原始内容,但也可通过
ng translude
指令将其重新插入模板中。见下面的例子

如果不半透明原始数据,锚定标记将被删除,这就是链接函数无法找到它们的原因

.directive('navigation', function () {
    return {
        template: '<div>Tansclude data here: <span ng-translude></span></div>',
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            title: '@'
        },
        link: function postLink(scope, element, attrs) {
            console.log($(element).find('a'));            
        }
   };
});
.directive('navigation',function(){
返回{
模板:“此处的Tansclude数据:”,
限制:'E',
替换:正确,
是的,
范围:{
标题:“@”
},
链接:函数postLink(范围、元素、属性){
log($(element.find('a'));
}
};
});

要在新模板中移动原始内容,需要使用
transclude
属性。当translude设置为true时,该指令将删除原始内容,但也可通过
ng translude
指令将其重新插入模板中。见下面的例子

如果不半透明原始数据,锚定标记将被删除,这就是链接函数无法找到它们的原因

.directive('navigation', function () {
    return {
        template: '<div>Tansclude data here: <span ng-translude></span></div>',
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
            title: '@'
        },
        link: function postLink(scope, element, attrs) {
            console.log($(element).find('a'));            
        }
   };
});
.directive('navigation',function(){
返回{
模板:“此处的Tansclude数据:”,
限制:'E',
替换:正确,
是的,
范围:{
标题:“@”
},
链接:函数postLink(范围、元素、属性){
控制台