I have a button on my page that modifies the model (adds a new object).
I was hoping I could do it within the HTML, simply by pushing a new object onto the array, but it looks like I may need to call the controler to do it. For some reason though it can't find the function in my controller.
Here's my button:
<button ng-click="addCarton()">Add a carton</button>
Here's one of the (several) controllers on this page:
myController.$inject = ['$http', '$uibModal', 'cart'];
function myController($http, $uibModal, cart) {
var vm = this;
vm.addCarton = function () {
console.log("carton added!");
});
This does nothing at all. No errors, just the button does nothing. I had no choice but to add the $scope dependency. This works:
myController.$inject = ['$http', '$uibModal', 'cart', '$scope'];
function myController($http, $uibModal, cart, $scope) {
var vm = this;
$scope.addCarton = function () {
console.log("carton added!");
});
So, why bother with vm=this if I can't use it?
--
I should mention there are other click events on the page, but I don't have any idea how they're operating:
<button type="button" ng-click="summaryModalVm.cancel()">Cancel</button>
function myController($http, $uibModal, cart, $scope) {
vm.dimensionsChoose = function (size) {
$http.post('/api/shipping/estimate', cart).then(function(response) {
var modalInstance = $uibModal.open({
controllerAs: 'summaryModalVm',
[ UPDATE ]
My router:
.state('layout.cart', {
url: '/cart',
templateUrl: '/Content/js/apps/store/views/cart.html',
controller: 'cartController',
controllerAs: 'cartVm',
data: { pageTitle: 'Cart' }
})
My 2nd try at a button:
<button ng-click="cartVm.addCarton()" type="button">Add a carton</button>
My 2nd try at controller:
function cartController($http, $uibModal, Cart, CartObservable) {
var vm = this;
vm.addCarton = function () {
console.log(":)");
};