从模板中获取ng-model值

Getting ng-model value from template

本文关键字:ng-model 获取      更新时间:2023-09-26

我有离子模态模板,看起来像:

<ion-modal-view>
    <ion-header-bar>
        .....
    </ion-header-bar>
    <ion-content>
        ...
        <input class="form-input" type="text" ng-model="relation"/>
        ...     
    </ion-content>
</ion-modal-view>
controller:

$scope.resolve = function () {
    console.log('relation', $scope.relation);
}

我需要从解析函数内的relation模型中获取值,但它是未定义的。我怎样才能得到它呢?

感谢关注!

这对我有效,请查看
我已经更新了你的Ionic-modal模板

<ion-modal-view>
    <ion-content>
        <input class="form-input" type="text" ng-model="test.relation"/>
        <button class="button button-bar button-positive" ng-click="answerswer()">Click Me</button>
    </ion-content>
</ion-modal-view> 

在您的控制器中,请将代码更新为:
$scope.test = {};
$scope.answer = function(){
    console.log($scope.test.relation);
}
如果您有任何疑问,请回复。

"如果你使用ng-model,你必须在那里有一个点。"

让你的模型指向一个对象。属性,你就可以走了。

导致这种情况的问题是嵌套状态。你可以在AngularJS中找到更多关于嵌套作用域的信息。

控制器

$scope.form = {};
$scope.resolve = function () {
    console.log('relation', $scope.form.relation);
}

测试模板

<ion-modal-view>
<ion-header-bar>
    .....
</ion-header-bar>
<ion-content>
    ...
    <input class="form-input" type="text" ng-model="form.relation"/>
    ...
</ion-content>