当嵌套在ion-content中时,HTML select不会绑定到控制器

时间:2014-12-03 12:35:07

标签: angularjs ionic-framework

我有一个带有select标签的小表单,表单在ion-content指令

现在虽然我可以使用{{selectedValue}}在视图上看到值,但是在控制器上它们是未定义的。我检查了代码,看起来很好。当我删除离子含量指令时,它开始工作得很好

这是我的代码(控制器在$ stateResolver中定义)

<ion-content>
<form>
    <select ng-model="selectedCity"
            ng-options="city as city.cityName for city in cities"
            ng-change="cityChanged()">
    </select>    
</form>

* ion-content标签正在关闭我不知道为什么堆栈会削减它

和js片段

controllers.controller('RegisterController',function($scope,$http,$state){

    $scope.formData = {
        firstName:'',
        lastName:'',
        email   :'',
        password:'',
        confirmPassword:'',
        phoneNumber:'',
        dob:'',
        country:{},
        selectedItem:''
    };
    $scope.errors = [];

    $scope.countries=[
            {countryId:1,countryName:"United States"},
            {countryId:2,countryName:"Mexico"}
        ];

    $scope.cities=[
            {cityId:1,countryId:1,cityName:'Miami'},
            {cityId:2,countryId:1,cityName:'Los Angeles'}   
        ];  

    $scope.cityChanged = function(){
        console.log($scope.selectedCity);
    }   

    $scope.register = function(){
        oformData = $scope.formData;
        oformData.selectedCity = $scope.selectedCity;

        console.log($scope.formData);
    }   
});
除非删除了ion-content指令,否则在控制台上它将显示undefined

这是一个错误吗?

1 个答案:

答案 0 :(得分:1)

它可能与ion-content指令有关,它创建了自己的隔离范围。 使用点表示法再次尝试(在处理范围继承时很重要)

例如,将ng-model更改为:

ng-model="form.selectedCity"

请参阅:

AngularJS documentation on scopes

Egghead video

如果它不起作用,请提供一个plunker来调试