Javascript 如何在具有动态输出的angular js中添加链接
我在这里有页面说类别,我正在从api请求加载类别 单击第一个类别,我需要加载一些其他页面,比如说产品,并需要从其他一些api加载产品。 当我用simple href链接它时,它会显示出来Javascript 如何在具有动态输出的angular js中添加链接,javascript,jquery,angularjs,controller,directive,Javascript,Jquery,Angularjs,Controller,Directive,我在这里有页面说类别,我正在从api请求加载类别 单击第一个类别,我需要加载一些其他页面,比如说产品,并需要从其他一些api加载产品。 当我用simple href链接它时,它会显示出来 {{product.productname}} MyController页面: var app = angular.module('myApp', []); app.controller('CategoryController', function($scope, $http) { $http.
{{product.productname}}
MyController页面:
var app = angular.module('myApp', []);
app.controller('CategoryController', function($scope, $http) {
$http.get("http://192.168.1.112/estore/api/get-categories")
.success(function(response) {$scope.categories = response.categories;});
});
app.controller('ProductController', function($scope, $http) {
$scope.id = 2;
$http.get("http://192.168.1.112/estore/api/get-products?id=id")
.success(function(response) {$scope.products = response.categories;});
});
我的索引页:
<body ng-app="myApp">
<div role="main" class="ui-content" ng-controller="CategoryController">
<ul class="cat" ng-repeat="x in categories">
<li>
<a href="list.html/{{x.id }}"> {{ x.name }} </a>
</li>
</ul>
</div>
</body>
-
我的列表页:
<body ng-app="myApp">
<div role="main" class="ui-content" ng-controller="ProductController">
<ul class="cat" ng-repeat="pdts in products">
<li>
{{ pdts.productname }}
</li>
</ul>
</div>
</body>
-
{{pdts.productname}
有两个选项:-
1) you can use $state.go() and pass id parameters to products state.
2) you can use $cookiestore and set the id in the cookie and later retrieve in the products state.
var app = angular.module('myApp', []);
app.config(['$stateProvider',function($stateProvider){
$stateProvider.state('products',{
url: '/products/:ID',
title: 'productlist',
templateUrl: 'app/views/product/productlist.html',
controller:'productlistController'
})
}]);
app.controller('CategoryController', function($scope, $http ,$state) {
$http.get("http://192.168.1.112/estore/api/get-categories")
.success(function(response){
$scope.categories = response.categories;
});
$scope.redirectTo = function(id){
$state.go('products', {ID : id});
};
});
app.controller('ProductController', function($scope, $http , $state) {
var id = $state.params.ID;
$http.get("http://192.168.1.112/estore/api/get-products?id="+id)
.success(function(response) {
$scope.products = response.products;
});
});
索引页
<body ng-app="myApp">
<div role="main" class="ui-content" ng-controller="CategoryController">
<ul class="cat" ng-repeat="x in categories">
<li>
<a ng-click="redirectTo(x.id)"> {{ x.name }} </a>
</li>
</ul>
</div>
</body>
-
{{x.name}
有两个选项:-
1) you can use $state.go() and pass id parameters to products state.
2) you can use $cookiestore and set the id in the cookie and later retrieve in the products state.
var app = angular.module('myApp', []);
app.config(['$stateProvider',function($stateProvider){
$stateProvider.state('products',{
url: '/products/:ID',
title: 'productlist',
templateUrl: 'app/views/product/productlist.html',
controller:'productlistController'
})
}]);
app.controller('CategoryController', function($scope, $http ,$state) {
$http.get("http://192.168.1.112/estore/api/get-categories")
.success(function(response){
$scope.categories = response.categories;
});
$scope.redirectTo = function(id){
$state.go('products', {ID : id});
};
});
app.controller('ProductController', function($scope, $http , $state) {
var id = $state.params.ID;
$http.get("http://192.168.1.112/estore/api/get-products?id="+id)
.success(function(response) {
$scope.products = response.products;
});
});
索引页
<body ng-app="myApp">
<div role="main" class="ui-content" ng-controller="CategoryController">
<ul class="cat" ng-repeat="x in categories">
<li>
<a ng-click="redirectTo(x.id)"> {{ x.name }} </a>
</li>
</ul>
</div>
</body>
-
{{x.name}
**MyController页面:**`var-app=angular.module('myApp',[]);app.controller('CategoryController',function($scope,$http){$http.get(“).success(function(response){$scope.categories=response.categories;});});app.controller('ProductController',function($scope,$http){$scope.id=2;$http.get(“).success(function(response){$scope.products=response.categories;});})`我建议你彻底地学习一下,以获得对角度如何工作的基本理解。例如,您不应该有两个带有ng app
指令的
标记。这不是应该是这样的:$http.get(“http://192.168.1.112/estore/api/get-products?id=“+id)
并坦率地说同意muenchdo.*MyController页面:*`var-app=angular.module('myApp',[]);app.controller('CategoryController',function($scope,$http){$http.get(“).success(function(response){$scope.categories=response.categories;});});app.controller('ProductController',function($scope,$http){$scope.id=2;$http.get(“).success(function(response){$scope.products=response.categories;});})`我建议你彻底地学习一下,以获得对角度如何工作的基本理解。例如,您不应该有两个带有ng app
指令的
标记。这不是应该是这样的:$http.get(“http://192.168.1.112/estore/api/get-products?id=“+id)
,坦率地说,同意muenchdo的观点。