【问题标题】:AngularJS Directive with no Template and Sharing the updates from parent scopeAngularJS 指令没有模板并从父范围共享更新
【发布时间】:2013-08-30 21:44:01
【问题描述】:

我有一个非常简单的要求,将指令放在 ng-repeat 中,我想将更改推送到指令,以便更新 UI。这意味着我希望在我的链接函数中使用 Contact(不仅仅是一个字符串引用实际对象) 的值,这样我就可以渲染 UI 并在 contact 中的 contact 更改时更改我的 UI父 ng-repeat

我已经创建了一个非常 simplified version 我想要实现的目标。它有一个联系人列表(在此 link by Mark 的帮助下进行研究后,我知道 ng-repeat 创建了自己的范围,所以我添加了一个名为 deep 的对象,它是一个对象不是原始的),我正在努力解决这个问题。我也曾尝试使用其他可能的选项。我不想用 ng-show/ng-class 东西在指令中粘贴模板,因为业务逻辑很棘手,所以使用链接功能更容易做到。我确信这与 scope 和/或 $observe 有关,但我不知道如何进行排序。另请注意,这是我想要实现的更简单的版本。我实际上希望该对象的值来呈现我的 UI。

My Fiddle Example 当您点击更新名称时,您会看到外部重复不会更新内部。

<div>
  <div ng-controller="ContactsCtrl">
    <ul>
        <li ng-repeat="contact in contacts">
            <div zippy="contact">a</div>            
        </li>
    </ul>
    <br />
    <p>Here we repeat the contacts to ensure bindings work:</p>
    <br />
    <ul>
        <li ng-repeat="contact in contacts">
            {{contact.name}} : {{contact.deep}}
        </li>
    </ul>    
  </div>
</div>



var app = angular.module( 'myApp', [] );
app.controller('ContactsCtrl', function ( $scope ) {      
    $scope.contacts = [
                        { name: 'Sharon',deep:{A:"a"}},
                        { name: 'Steve',deep:{A:"b"}},
                        { name: 'Scott',deep:{A:"c"}}
    ];

    $scope.primitiveTest=function(c){c.name=c.name+c.name; }    
    $scope.objectTest=function(c){c.deep={A:c.deep.A+c.deep.A};  }
});
app.directive('zippy',  ['$compile', function ($compile){
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {       

          /*scope.$watch(attrs.zippy, function(newValue, oldValue){
                alert('asd');
            });

          attrs.$observe('deep',function(val){ alert('asd'); });*/
        scope.$watch(attrs['zippy'],
             function (ideaNode) {      
                 var html = [];
                 html.push('<hr/><div class="clearfix" id="vote-icons" ng-click="primitiveTest('+attrs['zippy']+');" >Update Name </div> - <div class="clearfix" id="vote-icons" ng-click="objectTest('+attrs['zippy']+');">UpdateObject');              
                 html.push('</div>');
                 html.push(JSON.stringify(ideaNode));                
                 element.html(html.join(''));
                 $compile(element.contents())(scope);
             }
        );
      }
    }
  }]);

【问题讨论】:

  • 我很好奇是否能够对此进行单元测试?

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat


【解决方案1】:

如果您采用以下方法,您可以删除指令中的$watch。

如果您希望对模型进行插值和更新,则应使用对象插值html.push("{{" + attrs['zippy'] + "}}"); 而不是序列化的纯文本字符串。

app.directive('zippy', ['$compile', function ($compile) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var html = [];
            html.push('<hr/><div class="clearfix" id="vote-icons" ng-click="primitiveTest(' + attrs['zippy'] + ');" >Update Name </div> - <div class="clearfix" id="vote-icons" ng-click="objectTest(' + attrs['zippy'] + ');">UpdateObject');
            html.push('</div>');
            html.push("{{" + attrs['zippy'] + "}}");
            element.html(html.join(''));
            $compile(element.contents())(scope);
        }
    }
}]);

Working Demo

【讨论】:

  • 谢谢!这样可行 。但在我的实际示例中,我需要 attrs['zippy'] 的值,它是一个多级对象,因此我可以根据其中的各种属性渲染 UI。有什么想法吗?
  • @Kusek 如果对象在范围内,可以试试html.push("{{" + scope[attrs['zippy']] + "}}");
猜你喜欢
  • 2014-09-01
  • 1970-01-01
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多