2017-02-20 53 views
0

在AngularJSAngularJS如何選擇並在頁面加載

目標初始化函數:我怎樣才能得到所有類別(按鈕)加以強調和初始化頁面加載或刷新(如可能使用NG-init或NG-選項)

控制器

$scope.selectCategory = function(newCategory) { 
    $scope.bottomValue = newCategory; 
    selectedCategory = newCategory; 
    $scope.selectedPage = 1; 

} 

$scope.categoryFilterFn = function(product) { 
    return selectedCategory == null || 
    product.category == selectedCategory; 
} 

$scope.getCategoryClass = function(category) { 
    return selectedCategory == category ? productListActiveClass : ""; 

} 

$scope.getCategory = function(category) { 
    return categorySelected == category; 

} 

HTML

<a ng-click="selectCategory()" class="btn btn-block btn-default btn-lg" style="margin-bottom:6px">All Categories</a> 



<a ng-repeat="item in data.products | filter:{edition: data.editions[whichEdition].name}| filter:approved=true | orderBy:itemOrder:direction | orderBy:'-category' | unique:'category' " ng-click="selectCategory(item)" class=" btn btn-block btn-default btn-lg" 
ng-class="getCategoryClass(item)"> 
       {{item}} 
</a> 
+1

你的意思是**要突出顯示並初始化**,你能詳細說明一下嗎? – Sravan

回答

2

我更喜歡在控制器中使用init函數而不是ng-init,因爲在ng-init之前調用了init函數,並且我已準備好所有內容進行渲染。 在你的視圖控制器中。

​​
2

所以,你想要做的頁面加載,寫在一個單一的方法的方法一樣$scope.init和使用這樣稱呼它

<div ng-controller="controllerName" ng-init="init()"> 

這是,如果你想要做的頁面加載的一切一切。

0

使用anguls的$ window.onLoad。

$window.onload = function(param) { 
    //your code here 
    alert(param); 
} 

或者你可以使用與NG-控制器NG-初始化指令 -

<div ng-controller="yourController" ng-init="yourMethod(param)"> 

這裏是yourController -

var app = angular.module('DemoApp', []) 
app.controller('yourController', function($scope) { 
    $scope.yourMethod = function(param) { 
    alert(param.toString()); 
    } 
}); 
0

可以使用Yauza說,或者你也可以用,,, ,

loadAllFunction() function loadAllFunction(){ //Your code }