【问题标题】:angular javascript ng-model not working有角度的javascript ng-model不起作用
【发布时间】:2015-09-28 00:00:27
【问题描述】:

这是我项目的一个小节:

<div class="col-md-3 col-sm-4">
    <checked-input
     pholder="{{'publications-customDomain' | translate}}"
     class="add-publisher-input"
     ng-model="customDomain.text"
     icon="glyphicon-globe"
     type="publicationCustomDomain"
     page="publications">
    </checked-input>
</div>
<div class="col-md-3 col-sm-4">
    <button ng-if="isBtnClassDisabled===false" class="add-domain btn btn-primary" ng-click="vm.addUserPublisher(currentTab)">
        <span class="glyphicon glyphicon-plus"></span>{{'publications-addDomain' | translate}}
    </button>
</div>

它的工作方式是,checked-input 验证输入的任何内容以确保它是一个 URL,一旦验证,isBtnClassDisabled 变为 false,因此第二个 div 中的按钮出现,并且您能够点击它。这会调用函数vm.addUserPublisher,但是,当我尝试从控制器记录this.scope.customDomainchecked-inputng-model)时,控制台会返回undefined。谁能解释为什么?谢谢。

编辑 1 - 我的控制器如下所示:

class mainPublicationsCtrl {
    private scope: any;
    private timeout: any;
    private modal: any;
    private route: any;
    private http: any;
    private mainScope: any;
    private selSiteServ: any;
    static $inject = ['$scope'];

    constructor($scope, $timeout, $http, $modal, $route, selSiteServ) {
        $scope.vm = this;
        $scope.isBtnClassDisabled = true;
        $scope.selectedItem = 0;
        $scope.countOfTabs = 1;
        $scope.customDomain = {
            text: ""
        }
        this.scope = $scope;
        ...
        addUserPublisher(tab: any) {
            console.log(this.scope.customDomain.text);
        }
        ...
 }

编辑 2 - &lt;checked-input&gt; 指令定义如下:

模板:

<div>
  <div class="input-info-wrap">
    <div class="input-inform state-normal"></div>
  </div>
  <div class="input-group">
    <span ng-if="icon != '' && !isFaIcon"
          class="input-group-addon glyphicon {{icon}} icon-inline btn-item">
    </span>
    <span ng-if="icon != '' && isFaIcon"
          class="input-group-addon fa {{icon}} fa-input-label-size icon-inline btn-item">
    </span>
    <input type="text" 
           class="form-control btn-item form-tab-input" 
           placeholder="{{pholder}}"
           ng-model="model"
           maxlength="20">
    <span class="input-group-addon glyphicon glyphicon-asterisk input-state"></span>
  </div>
</div>

Java脚本:

class checkedInput implements ng.IDirective {
  public link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => void;
  public templateUrl = 'partials/directives/checked-input.html';
  public scope = { 
    model: "=",
    icon: "@",
    pholder: "@",
    type: "@",
    page: "@",
    num: "@",
    gstab: "=",
    gsrow: "=",
    tab: "=",
    isFaIcon: "="
  };
}

【问题讨论】:

  • 如果你只登录scope.customDomain呢?
  • 控制器被定义为一个类,所以我不确定这是否可行,无论如何我都会尝试一下。我会将它作为编辑添加到我的原始帖子中。
  • 是你写的指令吗?如果是这样,您可以将其发布在问题中吗?
  • @VSO 已更新以包含更多信息。
  • 谢谢。我看不出有什么问题,但希望它能帮助其他人看到这个问题。

标签: javascript html angularjs angular-ngmodel angularjs-ng-model


【解决方案1】:

尝试使用checked-input 指令的模板。在指令模板中定义ng-model,而不是在 HTML 中显式定义。

【讨论】:

    猜你喜欢
    • 2016-10-05
    • 2016-11-24
    • 2014-08-20
    • 1970-01-01
    • 2016-05-02
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多