如何使Javascript代码引用相同的attribue

时间:2015-08-03 03:30:38

标签: javascript angularjs

以下是AngularJS代码的Javascript代码段:

angular.module('myApp')
.config(function ($stateProvider) {
    $stateProvider
       .state('lodge', {
    ...
       }
        .state('lodge.new', {
            parent: 'lodge',
            url: '/new',
            data: {
                roles: ['ROLE_USER'],
            },
            onEnter: ['$stateParams', '$state', '$modal', function($stateParams, $state, $modal) {
                $modal.open({
                    templateUrl: 'scripts/app/entities/lodge/lodge-dialog.html',
                    controller: 'LodgeDialogController',
                    size: 'lg',
                    resolve: {
                        entity: function () {
                          var price = {currency: null, value: 0};
                          return {id: null, description: null, transportationHourlyRate: price, houseCleaningHourlyRate: price);
                      }
                    }
                }).result.then(function(result) {
                    $state.go('lodge', null, { reload: true });
                }, function() {
                    $state.go('lodge');
                })
            }]
        })
   .state('lodgeXXX', {
    ...
       }
  ...

在使用上述代码的html页面上,基于用户持续输入数据的两种费率的价格将相同。如果我只想要货币相同而不是价值,那么如何修改代码?我尝试了一些类似下面的方法而没有运气。

var currency_var = {currency: null};
var price1 = {currency: currency_var, value: 0},  price2 = {currency: currency_var, value: 0};
return {id: null, description: null, transportationHourlyRate: price1, houseCleaningHourlyRate: price2);

html端代码类似于以下内容:

 <div class="form-group">
        <label translate="myApp.lodge.transportationHourlyRate.currency" for="field_transportationHourlyRate_currency">TransportationHourlyRateCurrency</label>
        <select class="form-control" name="transportationHourlyRate.currency" ng-model="lodge.transportationHourlyRate.currency" id="field_transportationHourlyRate_currency">
            <option value="CAD">CAD</option>
            <option value="USD">USD</option>
        </select>

        <div ng-show="editForm.transportationHourlyRate.currency.$invalid">
            <p class="help-block"
               ng-show="editForm.transportationHourlyRate.currency.$error.required" translate="entity.validation.required">
                This field is required.
            </p>
        </div>
    </div>
    <div class="form-group">
        <label translate="myApp.lodge.transportationHourlyRate.value" for="field_transportationHourlyRate_value">TransportationHourlyRateCurrency</label>
        <input type="text" class="form-control" name="transportationHourlyRate.value" ng-model="lodge.transportationHourlyRate.value" id="field_transportationHourlyRate_value">
        </input>
    </div>

html代码结构与houseCleaningHourlyRate相同。

2 个答案:

答案 0 :(得分:0)

您的示例已关闭,但您不想将对象值分配给currency_var,而只需要:

var currency_var = null;

然后,您可以使用该变量将相同的值应用于价格变量“currency键。但是,如果没有看到更多代码,我不确定这些因素会如何影响您的Angular应用程序。

答案 1 :(得分:0)

执行以下操作时,您可以通过检查值来查看问题:

var currency_var = {currency: null}

您正在创建一个具有属性currency的对象,因此该对象本身不为null。如果您选中typeof(currency_var),则会得到object而不是null

只需改变:

var price1 = {currency: currency_var, value: 0}, ...

有:

var price1 = {currency: currency_var.currency, value: 0}, ...