我试图在我的控制器中添加一个post方法,但是我一直遇到一个我无法追踪的语法错误。我的目标是让一个控制器在init上获取(),然后在控制器中有按钮,可以在点击时调用additem(itemid)。
这是我的app.js:
(function(angular) {
'use strict';
angular.module('myApp', [])
.controller('ItemsController', ['$scope', '$http', '$log',
function($scope, $http) {
$scope.method = 'GET';
$scope.url = '/path/to/api';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
var response = $http({method: $scope.method, url: $scope.url}).
then(function(response) {
$scope.status = response.status;
$scope.data = response.data;
$scope.thedata = JSON.stringify(response.data);
$scope.count = $scope.data.length
console.log($scope.thedata);
console.log($scope.data[0]);
}, function(response) {
$scope.data = response.data || "Request Failed";
$scope.status = response.status;
})
};
$scope.additem = function(itemid) {
var itemdata = new Object();
itemdata.itemid = itemid;
$http.post($scope.url, itemdata).success(function(data){
//Callback function here.
//"data" is the response from the server.
if (data.status === "success") {
console.log("Added!")
}
});
}
}]),
]); // SyntaxError: expected expression, got ']'
})(window.angular);
就在前一行(window.angular);我在Firefox控制台中收到此错误:
SyntaxError: expected expression, got ']'
我很确定我的缩进在结束时有点混乱,因此很难分辨导致它的原因......
答案 0 :(得分:0)
结束这样的脚本纠正了语法:
};
}
]);
});
(window.angular);