ng-show使用AngularJs在自定义指令中不起作用

时间:2016-01-05 12:01:41

标签: angularjs angularjs-directive

我的问题是,我想根据json参数显示和隐藏文本框, 这里我得到json值(true,false)但ng-show在绑定值时不起作用 谢谢你的帮助

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

    app.directive('telBasictext1', ['$http', 'telngshowservice', function($http, telngshowservice) {
      return {
        restrict: 'AEC',
        require: 'ngModel',
        scope: {
          ngModel: '=',
          placeHold: '@',
          checkId: '@',
          className: '@',
          ngmaxLength: '@',
          ngminLength: '@',
          lblvalue: '@',
          textboxSize: '@',
          lblSize: '@',
          validate: '@',
          ngShow: '@',
          textboxtype: '@',
          getString: '@',
          position: '@',
          labelPosition: '@',
          textboxPosition: '@',
          canShow: '@',
          showorhide: '@',
        },
        template: '<div   id="{{ checkId }}" class="form-group" ng-show="true"  > ' +
          '<label size="lblSize"  class="col-sm-{{ labelPosition }} control-label" id="textboxchanges">   Test </label>' +
          '<div class="col-sm-{{ textboxPosition }}"> <input type="{{ textboxtype }}" ng-model="ngModel" placeholder="{{ placeHold }}"  id="{{checkId}}"   class="{{className}}"  minlength="{{ ngminLength }}"  maxlength="{{ ngmaxLength }}"  size="{{ textboxSize }}"           ng-required="{{ validate }}" ></div></div>',

        link: function(scope, iElement, iAttrs, ngModelController) {



          var ngshow = iAttrs.canShow;
          var ngsplitValues = ngshow.split(",");
          var nglanguage = ngsplitValues[0]; // Language EN or Fr
          var nglabelName = ngsplitValues[1]; // Label Name
          var ngmoduleName = ngsplitValues[2]; // Module Name (global or local)

          telngshowservice.getdata(ngmoduleName).success(function(data) {

            scope.showorhide = data[nglabelName];
            console.log(scope.showorhide)


          })


        }
      };
    }]);



    app.factory('telngshowservice', ['$http', function($http) {
      var dataFactory = {};
      var lang = window.localStorage.language;
      dataFactory.getdata = function(moduleName) {

        if (moduleName == 'common') {

          return $http.get(labeli18nPath + '/translation_' + lang + '.json');
        } else {


          return $http.get('OPlayout.json');
        }
      };
      return dataFactory;
    }]);

请查看以下网址

http://plnkr.co/edit/J201Y11ojOz2mTTeP524?p=preview

1 个答案:

答案 0 :(得分:1)

因为您使用文本绑定(@)在隔离范围中定义了hideorshow。注意:使用文本绑定时,双向数据绑定不起作用:)

删除它,它只是工作(或使用另一个范围变量)

工作示例:http://plnkr.co/edit/h3MrWQjopbqzYa0Y5pOT