【问题标题】:Angular 1.5 Custom directive to ComponentAngular 1.5 组件的自定义指令
【发布时间】:2016-05-30 06:37:54
【问题描述】:

我已经升级到 Angular 1.5,它现在支持 .component() 帮助器方法,以帮助用户过渡到 AngularJs 2。不幸的是,没有太多关于它的教程。我有以下简化的自定义指令和模板 URL。谁能帮我用.component() 的形式写这个?通过这样做,我应该了解它的基础知识,并且应该能够将它用于更复杂的指令。提前致谢。

指令

angular.module("formText", [])
.directive("formText",['$http','formService','$mdDialog', function($http,formService,$mdDialog){

    return{

                scope:{headId:'&',view:'='},
                link: function(scope,element,attrs){ 
                    scope.show = false;
                    scope.formService = formService;

                    scope.$watch('formService', function(newVal) {
                        scope.content = formService.getContent();                       
                    });
                },
                restrict:"E", 
                replace:true,
                templateUrl:"partials/form/tpl/element/text.html",
                transclude:true
            }//return
}])

模板网址

<div layout='column' flex>
     <md-input-container class="md-block" ng-if="view=='DRAFT'">
        <label>{{label()}}</label>
        <textarea style="border-bottom-color:#FFFFFF;" ng-model="content.value" columns="1" ></textarea>
      </md-input-container>
      <md-input-container class="md-block" ng-if="view=='READ'">
        <label>{{label()}}</label>
        <textarea style="border-bottom-color:#FFFFFF;" ng-model="content.value" columns="1" readonly></textarea>
      </md-input-container>
      <ng-transclude></ng-transclude>
</div>

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    directivecomponent 的转换代码如下所示。

    angular.module("formText", [])
    .component("formText", [function() {
        return {
          //bindings will bind all values to directive context       
          bindings: {
            headId: '&',
            view: '='
          },
          //link fn is deprecated now
          controller: function(formService, $element) {
            //but not sure how to do DOM manipulation, 
            //you could have $element for play with DOM
            var vm =this;
            vm.show = false;
            vm.formService = formService;
            vm.$watch(function(formService){ return formService}, function(newVal) {
              vm.content = formService.getContent();
            });
          },
          controllerAs: 'vm',
          //restrict: "E", //by default component will E
          //replace: true, //this has been deprecated
          templateUrl: "partials/form/tpl/element/text.html",
          transclude: true
        }
    }])
    

    模板

    <div layout='column' flex>
         <md-input-container class="md-block" ng-if="vm.view=='DRAFT'">
            <label>{{vm.label()}}</label>
            <textarea style="border-bottom-color:#FFFFFF;" ng-model="vm.content.value" columns="1" ></textarea>
          </md-input-container>
          <md-input-container class="md-block" ng-if="vm.view=='READ'">
            <label>{{vm.label()}}</label>
            <textarea style="border-bottom-color:#FFFFFF;" ng-model="vm.content.value" columns="1" readonly></textarea>
          </md-input-container>
          <ng-transclude></ng-transclude>
    </div>
    

    我建议你通过 Todd Motto 的 this article

    【讨论】:

    • 我尝试了上面的格式,但是得到一个注入器错误如下:Uncaught Error: [$injector:modulerr] errors.angularjs.org/1.5.0/$injector/…...at%20Ac.c%20(http%3A%2F%2Flocalhost% 2Flib%2Fjs%2Fangular.min.js%3A20%3A449) 。我的代码是:
    • angular.module("report", []) .component("report", [function() { return { //绑定将所有值绑定到指令上下文绑定:{}, //现在不推荐使用链接 fn controller: function(formService, $element) { //但不确定如何进行 DOM 操作,//你可以使用 $element 来玩 DOM }, controllerAs: 'vm', //restrict: " E", //默认组件将 E //replace: true, //这已被弃用 templateUrl: "partials/report/report.html", transclude: true } }])
    • @KaTech 你能给我plunkr吗?这样我就可以挖进去了。
    • 没关系,我让它工作了。我删除了函数并从组件返回。工作 JSFiddle jsfiddle.net/bkarv/33svc5ep
    猜你喜欢
    • 2016-05-16
    • 2017-08-16
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多