【问题标题】:Ionic Framework (AngularJS) Controller cannot see the declared ng-model离子框架(AngularJS)控制器看不到声明的 ng-model
【发布时间】:2015-08-07 02:02:20
【问题描述】:

这是我的 HTML 代码

<ion-view view-title="Home" ng-controller="makeOrderController">
    <input type="search" placeholder="Tìm kiếm" ng-model="searchKeyword" ng-change="searchMenu()" >
</ion-view>

这是我的 JS 代码

....
.controller('makeOrderController', ['$scope', function ($scope) {

    $scope.searchMenu = function ($scope) {
        console.log($scope.searchKeyword);
    }

}]);

是的。当我在搜索文本框上键入时,会执行 searchMenu() 方法,但它会引发错误

Cannot read property 'searchKeyword' of undefined

我已经搜索过,我试过了:

  • $parent.添加到ng-model

  • 或者使用js语句是$scope.model.searchKeyword。但是这些代码不起作用:(

请帮助我帮助 console.log 在用户类型上写入更新的关键字。

【问题讨论】:

  • 您应该使用 JSLint 或 JSHint 来运行您的代码;如果您使用 yeoman ionic-generator 模块,它是内置的。这将捕获这些错误。
  • 哦,我不知道这个。我会尝试探索它:D 非常感谢 :)

标签: javascript angularjs html ionic


【解决方案1】:

您有 2 个$scope 变量。所以最里面的$scope 采用的是未定义的偏好。

摆脱它..

.controller('makeOrderController', ['$scope', function ($scope) {

    $scope.searchMenu = function () {
        console.log($scope.searchKeyword);
    }

}]);

Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 2018-10-11
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多