ng-src和ng-href等同於html中的src和href,只是ng是在dom載入完畢之後才能夠發揮作用,如果在src或href中繫結變數,如果ng沒有載入完,會導致連結地址出錯,如果用對應的ng-src或ng-href,則不會出現這個問題。下面是一段示例程式碼:
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>interval</title> <script type="text/javascript" src="angular.min.js" charset="utf-8"></script> </head> <body ng-controller="ctrl"> <div><a ng-href="NewFile3_{{i}}.htm">連結</a></div> <div><input type="button" ng-click="clickEvent(1)" value="切換連結1" /><input type="button" ng-click="clickEvent(2)" value="切換連結2" /><input type="button" ng-click="clickEvent(3)" value="切換連結3" /></div> <script type="text/javascript"> var myApp = angular.module('myApp',[]); myApp.controller('ctrl', function($scope){ $scope.i = 1; $scope.clickEvent = function(index) { $scope.i = index; } }); </script> </body> </html>