如何将数据从角度传递到节点控制器?

时间:2016-01-29 11:13:02

标签: javascript angularjs node.js

我的前端html和控制器:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope,$http) {
	$scope.selectedBrands = [];
  
  $scope.selectBrand = function(selectedPhone) {
  	// If we deselect the brand
  	if ($scope.selectedBrands.indexOf(selectedPhone.brandname) === -1) {
    	// Deselect all phones of that brand
    	angular.forEach($scope.phones, function(phone) {
      	if (phone.brandname === selectedPhone.brandname) {
        	phone.selected = false;
        }
      });
    }
  }
  
  $scope.checkSelectedPhones = function() {
  	var modelNames = [];
    var aletrMsg= '';
  	angular.forEach($scope.phones, function(phone) {
    	if (phone.selected) {
      	modelNames.push(phone);
        aletrMsg += 'Brand : '+ phone.brandname + 'Phone Name: '+ phone.modelname + ' : Price: '+ phone.price +', ';
      }
    });
    alert(modelNames.length ? aletrMsg : 'No phones selected!');
  }
  var brand ="Nokia";
  $http.get('http://192.168.1.4:3000/shops1').success(function(data, status,response)
{ 
  
console.log(data);
});

	$scope.phones = [{
    id: "986745",
    brandname: "Lumia",
    modelname: "Lumia 735 TS"
  }, {
    id: "896785",
    brandname: "Lumia",
    modelname: "Lumia 830"
  }, {
    id: "546785",
    brandname: "Lumia",
    modelname: "Lumia 510"
  }, {
    id: "144745",
    brandname: "Asha",
    modelname: "Asha 230"
  }, {
    id: "986980",
    brandname: "Asha",
    modelname: "Asha Asn01"
  }, {
    id: "586980",
    brandname: "Asha",
    modelname: "Nokia Asha Dual sim"
  }, {
    id: "986980",
    brandname: "Asha",
    modelname: "Asha 540"
  }];
});

myApp.filter('unique', function() {
  return function(collection, keyname) {
    var output = [], 
        keys = [];

    angular.forEach(collection, function(item) {
      var key = item[keyname];
      if(keys.indexOf(key) === -1) {
        keys.push(key);
        output.push(item);
      }
    });

    return output;
  };
});
<div ng-controller="MyCtrl">
  <button ng-click="checkSelectedPhones()">
    Check selected phones
  </button>
  
  <div ng-repeat="phone in phones | unique:'brandname'">
    <label>
      <input type="checkbox" ng-true-value="'{{phone.brandname}}'" ng-false-value="''" ng-model="selectedBrands[$index]" ng-change="selectBrand(phone)">  
      {{phone.brandname}}
    </label>
  </div>
  
  <br>
  
  <div ng-repeat="brand in selectedBrands track by $index" ng-if="brand">
    {{brand}}
    <div ng-repeat="phone in phones" ng-if="phone.brandname === brand">
      <label>
        <input type="checkbox" ng-model="phone.selected" >  
        {{phone.modelname}}
        
      </label>
    </div>
  </div>
</div>

我在节点(express.js)中创建了路由和控制器。我正在努力为此传递数据 例如,我试过它的工作,但从htm我不知道如何传递值

find({
    'Categories.brandname':'Nokia',
    'Categories.models.name':'Lumia',
     'Categories.models.submodel.name':
    {'$all':[ 'Lumia 735 TS', 'Lumia 510']}
})

动态地我需要传递brandname,models.name,models.submodel.name的数据 我已经附上我的html。from html如何传递数据以供查找?任何人帮助我

我的后端路线:

app.route('/shops1/:shopPhone')
        .get(shops.read)
        .put(shops.update)
        .delete(shops.delete);

    // Finish by binding the Shop middleware
    app.param('shopPhone', shops.shopByPhone);

控制器:

exports.shopByPhone= function(req, res) {

 Shop.find({
    'Categories.brandname':'Nokia',
    'Categories.models.name':'Lumia',
     'Categories.models.submodel.name':
    {'$all':[ 'Lumia 735 TS', 'Lumia 510']}
}).sort('-created').populate('user', 'displayName').exec(function(err, shops) {
                if (err) {
                         return res.status(400).send({
                                 message: errorHandler.getErrorMessage(err)
                         });
                } else {
                         res.jsonp(shops);
                 }
         });
 };

如果我从控制器传递静态数据我得到的数据是我预期但我不知道如何从角度传递 检查我的演示品牌我已经提到demo

0 个答案:

没有答案