代码首先通过$ scope.options而不是$ http.get

时间:2017-04-11 06:50:10

标签: angularjs ionic-framework

我有一个带有3个幻灯片的离子滑动组件。在第一次运行应用程序时,所有三个幻灯片都会加载。然而,转到另一个控制器并返回到离子滑动所在的控制器(使用$ state.go),只显示一张幻灯片并出现以下错误:

TypeError: Cannot read property '0' of undefined

看到这个错误,我追踪它首先通过这一行:

sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx]
.CardNumber);

取代此代码获取卡号:

    var url = 'http://10.10.9.169/UserService3/WebService1.asmx';
    $http.get(url + '/getCardsbyUsername' + '?unameID=' + currentID ).success(function(response) {
    // stuff
        console.log('response is jsonobj = ' + response);
        var strObj = JSON.stringify(response).replace(/"(\w+)"\s*:/g, '$1:');
        var myObject = eval('(' + strObj + ')');
         $scope.cardNumbers = myObject;
         console.log('response is jsonarr = ' + $scope.cardNumbers);
    })
    .error(function(response) {
    // error stuff
        console.log('response error is = ' + response);
    });

这里是完整的代码:

$scope.currentIdx = 0;

var currentID = sharedProperties3.getUserID();
console.log('current ID = ' + currentID);

var url = 'http://10.10.9.169/UserService3/WebService1.asmx';
    $http.get(url + '/getCardsbyUsername' + '?unameID=' + currentID ).success(function(response) {
    // stuff
        console.log('response is jsonobj = ' + response);
        var strObj = JSON.stringify(response).replace(/"(\w+)"\s*:/g, '$1:');
        var myObject = eval('(' + strObj + ')');
         $scope.cardNumbers = myObject;
         console.log('response is jsonarr = ' + $scope.cardNumbers);
    })
    .error(function(response) {
    // error stuff
        console.log('response error is = ' + response);
    });

  $scope.options1 = {
    initialSlide: 0,
    onInit: function(slider1)
    {
        $scope.slider1 = slider1;
        sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx].CardNumber);

    },
    onSlideChangeEnd: function(slider1)
    {
        console.log('The active index is ' + slider1.activeIndex); 
        $scope.currentIdx = slider1.activeIndex;
        console.log('The active card is ' + $scope.cardNumbers[$scope.currentIdx].CardNumber); 
        sharedProperties.setProperty($scope.cardNumbers[$scope.currentIdx].CardNumber);
    }
    };

  $scope.options2 = {
  direction: 'vertical',
  slidesPerView: '1',
  pagination: false,
  initialSlide: 1,
  showNavButtons: false
  };

如何让它首先通过$ http.get代码块?

1 个答案:

答案 0 :(得分:0)

我注意到我不止一次加载angularjs,所以我摆脱了在index.html中调用angularjs的额外代码,并且它有效。