AngularJS对象未更新

时间:2016-03-14 12:32:49

标签: javascript angularjs

我有一个这样的对象:

Exception:03-14 17:39:19.268: E/AndroidRuntime(1236): java.lang.RuntimeException: Unable to
instantiate application com.salesforce.androidsdk.phonegap.app.HybridApp: 
java.lang.ClassNotFoundException: Didn't find class 
"com.salesforce.androidsdk.phonegap.app.HybridApp
va.lang.ClassNotFoundException: Didn't find class 
"com.salesforce.androidsdk.phonegap.app.HybridApp" on path: 
DexPathList[[zip file "/data/app/com.test-8.apk"],
nativeLibraryDirectories=[/data/app-lib/com.test-8, /vendor/lib, /system/lib]]

首次启动控制器时, .controller('lineCtrl', function($sessionStorage, $rootScope, $scope, $stateParams, $state, $filter, config, $http, SCAN_EVENT, $ionicLoading, $ionicPopup, ScanService){ var areaName; var lineCounts; function filterObjects () { switch ($stateParams.area) { case 'PW': $scope.items = $filter('filter')($rootScope.runnerItemsPW, {line: $stateParams.line}, true); areaName = "Putwall"; lineCounts = $rootScope.lineCountPW; break; case 'NC': $scope.items = $filter('filter')($rootScope.runnerItemsNC, {line: $stateParams.line}, true); areaName = "Non-Con"; lineCounts = $rootScope.lineCountNC; break; case 'RE': $scope.items = $filter('filter')($rootScope.runnerItemsRE, {line: $stateParams.line}, true); areaName = "Receiving"; lineCounts = $rootScope.lineCountRE; break; case 'SP': $scope.items = $filter('filter')($rootScope.runnerItemsSP, {line: $stateParams.line}, true); areaName = "Singles Pack"; lineCounts = $rootScope.lineCountSP; break; default: break; } } filterObjects(); $rootScope.lineData = { position: 1, lineNumber: $stateParams.line, area: $stateParams.area, areaName: areaName, lineCounts: lineCounts, items: $scope.items }; $scope.clearLine = function(){ ScanService.dispatch(String("00" + $rootScope.lineData.lineNumber).slice(-2) + 'DONE'); }; $scope.removeStationItems = function (stationdID, createdDateTime){ var url = config.baseURL + "/runner/removeDesktopRunnerItem/" + stationdID + "/" + createdDateTime.split(' ').join('_') + "/" + $sessionStorage.user.loginName; $http.get(url).then(function(response){ if(response.status === 200 && response.statusText === "OK"){ var data = response.data; if(data === ''){ $state.go($rootScope.prevState); $rootScope.setObjects(data); filterObjects(); }else{ $rootScope.setObjects(data); filterObjects(); var found = $filter('getByStationID')(data.runnerItems, $rootScope.lineData.lineNumber); if(found === null){ $state.go($rootScope.prevState); } } } }) }; }) .controller('queue', function ($rootScope, $http, $filter, $scope, config, $interval, $ionicLoading) { $rootScope.setObjects = function (data){ $rootScope.runnerItemsPW = $filter('filter')(data.runnerItems, {areaShort: "PW"}); $rootScope.lineCountPW = $filter('filter')(data.lineCounts, {areaShort: "PW"}); $rootScope.runnerItemsRE = $filter('filter')(data.runnerItems, {areaShort: "RE"}); $rootScope.lineCountRE = $filter('filter')(data.lineCounts, {areaShort: "RE"}); $rootScope.runnerItemsNC = $filter('filter')(data.runnerItems, {areaShort: "NC"}); $rootScope.lineCountNC = $filter('filter')(data.lineCounts, {areaShort: "NC"}); $rootScope.runnerItemsSP = $filter('filter')(data.runnerItems, {areaShort: "SP"}); $rootScope.lineCountSP = $filter('filter')(data.lineCounts, {areaShort: "SP"}); }; }); 会添加到对象中,并且工作正常。当新数据进入时,行$scope.items会更新。但是,$scope.items的ng-repeat不会更新。

4 个答案:

答案 0 :(得分:1)

$ filter创建一个新对象,因此需要更新$ rootScope.lineData。过滤后再次更新$ rootScope.lineData。

答案 1 :(得分:1)

重新分配$scope.items时,您正在破坏原始对象引用,因此传递给lineData对象的原始引用不再与$scope.items引用的数组相同

简单示例

var myArray = [1,2,3];

var obj={
   items: myArray // reference to array above 
}
console.log(myArray ); // [1,2,3]
console.log(obj.items); // [1,2,3] - exact same object reference as myArray

// now reassign myArray with new object reference 
myArray =[6,7,8];

console.log(myArray ); // [6,7,8]
console.log(obj.items); // [1,2,3] - the original reference hasn't changed

// now reassign object property value to new array
obj.items = myArray ;
console.log(obj.items); // [6,7,8] - references new array

注意:您应该使用服务跨控制器共享数据和方法。

答案 2 :(得分:0)

您只在控制器初始化函数中分配给$rootScope.lineData一次。在引导过程中,此函数仅被调用一次。

您还需要在filterObjects功能中分配它,以便通过$http电话更新它。这意味着您需要在第一次调用filterObjects之前创建它。像这样:

$rootScope.lineData = {
    position: 1,
    lineNumber: $stateParams.line,
    area: $stateParams.area,
    areaName: areaName,
    lineCounts: lineCounts,
};

function filterObjects () {
    switch ($stateParams.area) {
        case 'PW':
            $rootScope.lineData.items = $scope.items = $filter('filter')($rootScope.runnerItemsPW, {line: $stateParams.line}, true);
            areaName = "Putwall";
            lineCounts = $rootScope.lineCountPW;
            break;
        // etc.
    }
}

filterObjects();

答案 3 :(得分:-2)

在$ http响应代码中添加行$ scope。$ apply();在调用函数filterObjects()

之后