【问题标题】:ng-init with words in AngularJSng-init 与 AngularJS 中的单词
【发布时间】:2015-06-06 15:23:19
【问题描述】:

我正在尝试设置正在创建的新项目是服务还是产品。我希望默认选择产品。

<div ng-init="pr.type=Product">
  <div class="form-group">
    <label class="radio-inline">
      <input type="radio" ng-model="pr.type" id="productType" value="Product"> Product
    </label>
    <label class="radio-inline">
      <input type="radio" ng-model="pr.type" id="serviceType" value="Service"> Service
    </label>
  </div>
</div>

如果我将 pr.type 更改为数字,例如 1 代表产品,2 代表服务,它就可以工作。但这不是我想要的。

因为我的字段占位符会随着所选类型的变化而变化..

<div class="row">
  <div class="col-sm-6">
    <div class="form-group">
      <input type="text" ng-model="pr.upc" class="form-control" placeholder="{{pr.type}} Number"/>
    </div>
  </div>
  <div class="col-sm-6">
    <div class="form-group">
      <input type="text" ng-model="pr.name" class="form-control" placeholder="{{pr.type}} Name"/>
    </div>
  </div>
</div>

ng-init 不能处理单词吗?我会觉得没关系。我做错了什么?

【问题讨论】:

  • 您的代码是否将单词引用为字符串,例如 `ng-init="pr.type='Product'"?
  • No.... 就像问题中一样。那会有什么不同吗?
  • @jcern 是对的,只需加上引号:&lt;div ng-init="pr.type='Product'"&gt;
  • @jcern 让一切变得不同。您介意创建一个答案以便我接受吗?每个堆栈溢出策略。

标签: jquery angularjs ng-init


【解决方案1】:

由于你的参数是一个字符串,你需要引用它,否则javascript引擎不会按照你的意愿解释它。

要修复它,只需在术语周围添加引号,如下所示:ng-init="pr.type='Product'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多