angularjs - Angular 1.5 对组件的自定义指令

标签 angularjs angularjs-directive

我已经升级到 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>

最佳答案

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>

我建议你通过 this article通过托德座右铭

关于angularjs - Angular 1.5 对组件的自定义指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475660/

相关文章:

angularjs - 是否可以将Testacular(Karma)与 Angular 方案混合?

javascript - Angular Charts.js : Can't refresh chart with data

angularjs - 在 app.js 外部定义路由

AngularJS : Directive not able to access isolate scope objects

javascript - Controller 和 postLink 初始化后重置指令中的范围变量;

javascript - 隔离仅限于属性的指令的范围

javascript - AngularJS - 观察者在重新加载/路由器更改期间触发回调?

javascript - AngularJS ui 路由器 $stateChangeStart 带有 promise 无限循环

javascript - ng-model 和 ng-value 之间有什么区别/不兼容?

javascript - 如何从另一个 Controller 函数调用具有不同 templateurl 和 Controller 的指令?