簡體   English   中英

如何在angularjs中調用指令

[英]How to call a directive in angularjs

我正在嘗試通過指令顯示模型框,我不確定我哪里出錯了,但我的指令根本沒有得到解決。有人可以建議幫助。謝謝。

    <div class="modal fade" id="institutionModal" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">     
            <common-directive-institution field="field" data="data"  ng-if="showInstitutionModal"></common-directive-institution>
        </div>
    </div>
</div>
 <button type="button" ng-click="editInstitutionModal('general')" class="btn btn-white">Edit</button>

我的js

$scope.editInstitutionModal = function (type) {
  $scope.field = {};
  $scope.showInstitutionModal = false;
  if (type === 'basicedit') {
        $scope.field.field_type = 'edit-institution.form.client';
        $scope.field.formName = 'Edit institution (' + vm.institutionObj.name + ')';
        $scope.field.saveText = 'Update';  
  }
  if(type === 'general'){
        $scope.field.field_type = 'add-genaral.form.client';
        $scope.field.formName = 'General Info';
        $scope.field.saveText = 'Save';          
  }
  $timeout(function () {
    $scope.showInstitutionModal = true;
    $('#institutionModal').modal('show');
    $scope.$apply();
  }, 10);
};

我的指令

    (function () {
  'use strict';

  angular
          .module('users')
          .directive('commonDirectiveInstitution', function ($http, $compile) {

            var getTemplateUrl = function (field) {           
              var type = field.field_type;
              var templateUrl = '/modules/institutions/client/views/';
              templateUrl += type + '.html';
              return templateUrl;
            };

            var linker = function (scope, element) {
              var templateUrl = getTemplateUrl(scope.field);
              $http.get(templateUrl).success(function (data) {
                element.html(data);
                element.removeAttr('style');
                $compile(element.contents())(scope.$parent);
              });
            };

            return {
              template: '<div style="display:none">{{field}}</div>',
              restrict: 'E',
              scope: {
                field: '='
              },
              replace: true,
              link: linker
            };
          });
}());

我正在嘗試通過指令顯示模型框,我不確定我哪里出錯了,但我的指令根本沒有得到解決。有人可以建議幫助。謝謝。

該字符串:

'add-genaral.form.client'

單詞“ general”有誤,您可以使用此字符串從url獲取模板

/modules/institutions/client/views/add-genaral.form.client.html

您確定正確嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM