最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to write custom 'row' and 'col' element for angularjs - Stack Overflow

programmeradmin0浏览0评论

I'm trying to write a little dsl around the grid elements outlined here: .php

basically what I wish to do is to

<row>
  <column two mobile-one>{{myText}}</col>
  <column four centered mobile-three><input type="text" ng-model="myText"></input></col>
</row>

transform into:

<div class="row">
  <div class="columns two mobile-one">{{myText}}</div>
  <div class= "columns four centered mobile-three"><input type="text" ng-model="myText"></input></div>
</div>

Ideally, I wish to write something that can take arbitrary nesting: row -> col -> row -> col -> row.....

I am having trouble getting the first step right - nesting the elements because I can't quite figure how how to get the child elements into another template without seriously promising the pilation process.

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

  app.directive('row', function(){
    return {
      restrict: 'E',
      pile: function(tElement, attrs) {
        var content = tElement.children();
        tElement.replaceWith(
          $('', {class: 'row',}).append(content));
      }
    }
  });

just does not do anything. The failed attempt is shown here - /

Please help!

I'm trying to write a little dsl around the grid elements outlined here: http://foundation.zurb./docs/grid.php

basically what I wish to do is to

<row>
  <column two mobile-one>{{myText}}</col>
  <column four centered mobile-three><input type="text" ng-model="myText"></input></col>
</row>

transform into:

<div class="row">
  <div class="columns two mobile-one">{{myText}}</div>
  <div class= "columns four centered mobile-three"><input type="text" ng-model="myText"></input></div>
</div>

Ideally, I wish to write something that can take arbitrary nesting: row -> col -> row -> col -> row.....

I am having trouble getting the first step right - nesting the elements because I can't quite figure how how to get the child elements into another template without seriously promising the pilation process.

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

  app.directive('row', function(){
    return {
      restrict: 'E',
      pile: function(tElement, attrs) {
        var content = tElement.children();
        tElement.replaceWith(
          $('', {class: 'row',}).append(content));
      }
    }
  });

just does not do anything. The failed attempt is shown here - http://jsfiddle/ZVuRQ/

Please help!

Share Improve this question edited Dec 31, 2012 at 23:39 zcaudate asked Dec 31, 2012 at 22:34 zcaudatezcaudate 14.3k7 gold badges68 silver badges134 bronze badges 1
  • 1 col is an HTML element, so you probably shouldn't/can't use it as a directive name. – Mark Rajcok Commented Dec 31, 2012 at 23:29
Add a ment  | 

2 Answers 2

Reset to default 7

I was hoping not to use ng-transclude because I found that it added an additional scope.

Here is a directive that does not use ng-transclude:

app.directive('row', function() {
    return {
        restrict: 'E',
        pile: function(tElement, attrs) {
            var content = angular.element('<div class="row"></div>')
            content.append(tElement.children());
            tElement.replaceWith(content);
        }
    }
});

You may want to use tElement.contents() instead of tElement.children().

You don't need jquery at all in your example (but you need to include it on your page/jsFiddle):

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


app.directive('row', function(){
    return {
      restrict: 'E',
      template: '<div class="row" ng-transclude></div>',
      transclude: true,
      replace: true
    };
});   
发布评论

评论列表(0)

  1. 暂无评论