$scope在ionic应用中返回undefined。

16 浏览
0 Comments

$scope在ionic应用中返回undefined。

我对$scope的双重行为感到困惑。在下面的脚本中,我将name的值作为弹出窗口显示。但是在我的ionic应用中,相同的代码却弹出了undefined

我在谷歌上搜索了这个问题,并在这个链接上找到了解决方案,其中指出我们需要在ng-model中使用点(.)来获取值。这两者有什么区别呢?

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.a =function a(){alert($scope.name);}
});


姓名: 

0
0 Comments

问题原因:控制器被重新实例化时,可能会导致$scope返回undefined。

解决方法:检查并确保控制器没有被重新实例化。

0
0 Comments

$scope returns undefined in ionic app的问题出现的原因是在controller函数中没有正确地定义$scope对象。解决方法是将controller函数中的定义改为如下形式:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.a =function(){
        alert($scope.name);
    }
});

现在,$scope.a =function a(){alert($scope.name);} ('function a')

0
0 Comments

在Ionic应用中,使用$scope时返回undefined的问题是由于子作用域的存在而引起的,例如子路由或ng-repeat。

子作用域创建了自己的值,从而导致名称冲突。可以通过在模型中添加一个点来解决这个问题。将模型指向一个对象的属性即可。

解决方法之一是在控制器中声明一个空对象:

$scope.formData = {};
$scope.check = function () {
  console.log($scope.formData.searchText.$modelValue); //works
}

在模板中使用ng-model指令时,确保模型指向$scope.formData.searchText。



另外,也可以使用this关键字替代$scope来解决这个问题。

其他解决方案可以参考以下链接:

- [Ng-model does not update controller value](https://stackoverflow.com/questions/12618342/22768720#22768720)

- [Why is my ng-model variable undefined in controller?](https://stackoverflow.com/questions/23455718/)

希望以上解决方案对你有所帮助!

0