2016-11-17 23 views
0

私はAngularJSを学びたいと思っています。古いチュートリアルでは簡単なアプリを作りました。これは、index.htmlと、ui-router経由でロードされる2つの部分的なビューを持っています。私はそれが別のファイルに分かれていないことを知っています - これは学習プロジェクトだけです。簡単な例ではスコープにアクセスできない

問題は、$ scopeがView1または呼び出されたJS関数で使用できないように見えるため、ng-repeatは何も表示されないように見え、addCustomerは$ scope.newCustomer.nameを見ることができません。私は何か簡単でないことを確信しています。

のindex.html:

<!DOCTYPE html> 
<html ng-app="demoApp"> 

<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    <title></title> 
    <script src="js/angular/angular.js"></script> 
    <script src="js/angular-ui-router/angular-ui-router.js"></script> 
    <!-- your app's js --> 
    <script> 

var demoApp = angular.module('demoApp', ['ui.router']); // [] is for dependencies 

// routes 
demoApp.config(function($stateProvider, $urlRouterProvider) { 
    var nameState = { 
    name: 'name', 
    url: '/view1', 
    controller: 'SimpleController', 
    templateUrl: 'partials/View1.html' 
    } 

    var cityState = { 
    name: 'city', 
    url: '/view2', 
    controller: 'SimpleController', 
    templateUrl: 'partials/View2.html' 
    } 

    $stateProvider.state(nameState); 
    $stateProvider.state(cityState); 

    $urlRouterProvider.otherwise('/view1'); 
}); 

// controller 

demoApp.controller("SimpleController", function ($scope, $log) { 
    $scope.log = $log; 

    $scope.customers = [{name: 'John Doe', city:'New York'}, 
         {name: 'Jake Smith', city:'Atlanta'}, 
         { name: 'Jane Doe', city:'San Francisco'}]; 


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

</script> 
</head> 

<body> 
    <div> 
    <!-- placeholder for views inserted by ui-router --> 
    <ui-view></ui-view> 
    </div> 
</body> 

</html> 

View1.html

<div class="container"> 
    <h2>View 1</h2> 
    Name: <input type="text" ng-model="filter.name" /> You entered: {{filter.name}} 
    <div class="container"> 
    <h3> Loop </h3> 
    <ul> 
     <li ng-repeat="cust in $scope.customers | filter:filter.name">{{ cust.name }} - {{ cust.city }}</li> 
    </ul> 
    <br /> Customer Name: 
    <input type="text" ng-model="newCustomer.name" /> 
    <br /> Customer City: 
    <input type="text" ng-model="newCustomer.city" /> 
    <br /> 
    <br /> 
    <button ng-click="addCustomer()">Add Customer</button> 
    </div> 
    <a href="#/view2">Switch to View 2</a> 
</div> 

答えて

1

のみを使用し

ng-repeat="cust in customers" 
+0

ありがとうございました。 –

1

あなたがで$範囲を指定する必要はありませんhtml:

ng-repeat="cust in customers" 
1

これを試してみてください。

<li ng-repeat="cust in customers"> 

あなたはいけない( "$スコープ" を削除します)これを試して$scope.customers

1

を言及する必要があります。

ng-repeat="cust in customers | filter:filter.name" 

の代わり:

ng-repeat="cust in $scope.customers | filter:filter.name"