最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular.js - ng-model overwrite my ng-value on text-input - Stack Overflow

programmeradmin1浏览0评论

I'll start off with mentioning I'm very new to Angular so I'm probably not doing things in the correct "Angular way".

I have the following ng-repeat looping through some data - the correct data is being returned.

<tr ng-repeat="attribute in attributeCategory.attributes">
    <td>
        {{attribute.name}}
    </td>
    <td>
        <input type="checkbox" ng-model="attr.enabled" ng-checked="{{attribute.parameters.enabled}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>
    <td>
        <input type="checkbox" ng-model="attr.normalise" ng-checked="{{attribute.parameters.normalise}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>                
    <td>
        <input type="checkbox" ng-model="attr.reverse" ng-checked="{{attribute.parameters.reverse}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>
    <td>
        <input type="text" ng-model="attr.min_threshold" ng-value="{{attribute.parameters.min_threshold}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>                    
    </td>
    <td>
        <input type="text" ng-model="attr.max_threshold" ng-value="{{attribute.parameters.max_threshold}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>                    
    </td>
<tr>

For some reason the ng-value on the input="text" fields doesn't show. I need to pass through the data from the field in to the updateAttribute() function, which is why I'm binding the model to the attr variable that I'm passing through. This variable is then being used for an API call.

If I take the model off the text fields I have the correct values, but I need someone of getting that value and passing it through to the function.

If there is another way that I should be doing this please let me know :)

I'll start off with mentioning I'm very new to Angular so I'm probably not doing things in the correct "Angular way".

I have the following ng-repeat looping through some data - the correct data is being returned.

<tr ng-repeat="attribute in attributeCategory.attributes">
    <td>
        {{attribute.name}}
    </td>
    <td>
        <input type="checkbox" ng-model="attr.enabled" ng-checked="{{attribute.parameters.enabled}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>
    <td>
        <input type="checkbox" ng-model="attr.normalise" ng-checked="{{attribute.parameters.normalise}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>                
    <td>
        <input type="checkbox" ng-model="attr.reverse" ng-checked="{{attribute.parameters.reverse}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>
    </td>
    <td>
        <input type="text" ng-model="attr.min_threshold" ng-value="{{attribute.parameters.min_threshold}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>                    
    </td>
    <td>
        <input type="text" ng-model="attr.max_threshold" ng-value="{{attribute.parameters.max_threshold}}" ng-change="updateAttribute(attr, {{attribute.attribute_id}})"/>                    
    </td>
<tr>

For some reason the ng-value on the input="text" fields doesn't show. I need to pass through the data from the field in to the updateAttribute() function, which is why I'm binding the model to the attr variable that I'm passing through. This variable is then being used for an API call.

If I take the model off the text fields I have the correct values, but I need someone of getting that value and passing it through to the function.

If there is another way that I should be doing this please let me know :)

Share Improve this question edited Jun 24, 2014 at 10:28 AndrewPolland 3,0714 gold badges30 silver badges39 bronze badges asked Jun 24, 2014 at 10:22 user3770721user3770721 231 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Ng-value is not ment for intput[text]

Binds the given expression to the value of input[select] or input[radio], so that when the element is selected, the ngModel of that element is set to the bound value.

See docs

Additionally ng-change expects expression so you do not have to use {{}}

use this instead

ng-change="updateAttribute(attr,attribute)"
发布评论

评论列表(0)

  1. 暂无评论