2016-03-04 61 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?