2016-03-04 45 views
0

嗨,大家好,我正在尝试学习Angular制作离子应用程序,我正在关注Angular的教程,事情并非所有的角度声明都适用于离子。我试着去的姓名和输入的城市推到一个数组(不需要保存将字符串推到离子阵列

这些都是我的输入字段:

<p>What is your first name? </p> 
<input type="text" ng-model="newCustomer.name"/> 
<p>What city do you live in?</p> 
<input type="text" ng-model="newCustomer.city"/> 
<button ng-click="addCustomer()">Become a customer</button> 

这是怎么回事名称和城市都显示:

<ul> 
    <li ng-repeat="cust in customers | filter: name"> {{cust.name + " from " + cust.city}} </li> 
</ul> 

这是控制器:

demoApp.controller('simpleController', function ($scope){ 
    $scope.customers = [ 
     { name: 'jasper', city: 'Amsterdam' }, 
     { name:'Dave',city:'phoenix'} , 
     { name:'Gina', city:'Amsterdam' }, 
     { name: 'Philip', city:'Otterloo'} 
     ]; 

    $scope.addCustomer = function() { 
     $scope.customers.push(
      { 
       name: $scope.newCustomer.name, 
       city: $scope.newCustomer.city 
     }); 
    }; 
}); 

我下面的教程,我已经搜查堆栈和谷歌,但真的找不到我做错了什么,我希望有人能帮助我。

P.S.我在堆栈上有点新,所以如果我做错了事情(堆栈规则明智),请不要犹豫,向我指出。

编辑:有人问全app.js代码

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

demoApp.config(function($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/') 

    $stateProvider.state('home', { 
    url: '/home', 
    views: { 
    home: { 
     templateUrl: 'home.html', 
     controller: 'simpleController' 
    } 
    } 
}) 

$stateProvider.state('help', { 
    url: '/help', 
    views: { 
    help: { 
     templateUrl: 'help.html', 
     controller: 'simpleController' 

    } 
    } 
}) 
}); 

demoApp.controller('simpleController', function ($scope){ 
    $scope.customers = [ 
     { name: 'jasper', city: 'Amsterdam' }, 
     { name:'Dave',city:'phoenix'} , 
     { name:'Gina', city:'Amsterdam' }, 
     { name: 'Philip', city:'Otterloo'} 
     ]; 

    $scope.addCustomer = function() { 
     $scope.customers.push(
      { 
      name: $scope.newCustomer.name, 
      city: $scope.newCustomer.city 
     }); 
    }; 
}); 
+0

尝试做一个console.log,看看你是否可以在模拟器中调试它 –

+0

感谢在控制台中得到了这个错误:“TypeError:无法读取未定义的属性'名称'在Scope。$ scope.addCustomer”在我的应用程序.js – jasper

+0

尚未找到解决方案 – jasper

回答

0

我找到了答案,没有我的代码是不正确的。 解决的办法是把:

$scope.newCustomer= {}; 

权下的控制器,这个问题是newCustomer是不确定的。

谢谢大家的帮助。

快乐编码!

编辑:积分到Khanh找到他在这个堆栈上的答案pos:Why ng-model is not defined?