AngularJs壓縮時需要注意的事項,AngularJs壓縮事項
由於AngularJS是通過控制器建構函式的參數名字來推斷依賴服務名稱的。所以如果你要壓縮控制器的JS代碼,它所有的參數也同時會被壓縮,這時候依賴注入系統就不能正確的識別出服務了。
假如我們的Controller的名稱為:BookCtrl,壓縮前的代碼為:
var BookCtrl = function($scope, $http) { /* constructor body */ };
為了克服壓縮引起的問題,只要在控制器函數裡面給$inject屬性賦值一個依賴服務識別符的數組:
BookCtrl.$inject = ['$scope', '$http'];
另一種方法也可以用來指定依賴列表並且避免壓縮問題——使用Javascript數組方式構造控制器:把要注入的服務放到一個字串數組(代表依賴的名字)裡,數組最後一個元素是控制器的方法函數:
var BookCtrl = ['$scope', '$http', function($scope, $http) { /* constructor body */ }];
上面提到的兩種方法都能和AngularJS可注入的任何函數完美協作,要選哪一種方式完全取決於你們項目的編程風格,建議使用數組方式。
AngularJs的select的回顯問題
不知道你的的aa及其一些資料格式是什麼樣的,下面是選中的例子看下:
<!DOCTYPE html><html ng-app><head><title>angular-selected</title></head><body ng-controller="TestCtrl"><select ng-model="color" ng-options="c.name for c in colors"></select><script type="text/javascript" src="js/angular.min.js"></script><script type="text/javascript">function TestCtrl($scope){ $scope.colors = [{name:'black', shade:'dark'},{name:'white', shade:'light'},{name:'red', shade:'dark'},{name:'blue', shade:'dark'},{name:'yellow', shade:'light'} ]; $scope.color = $scope.colors[2];}</script></body></html>
AngularJS環境搭建
注意我下面標的重點區就可以了,
1、ng-app加上
2、把angularjs引入就可以了
具體到 angularjs.org/ 這裡看
<!doctype html><html ng-app> //重點1<head> <meta charset="UTF-8"> <title>angularjs</title></head><body> <div> <label>Name:</label> <input type="text" ng-model="yourName"placeholder="Enter a name here"> <hr> <h1>Hello {{yourName}}!</h1> </div><script src="ajax.googleapis.com/...min.js"></script>//重點2</body></html>