Angular-ui路由器不更新链接

时间:2014-07-15 13:37:30

标签: angularjs angular-ui-router

我正在构建一个基本的RSS阅读器,我有一个基本的表单,它将一系列url上传到下拉菜单中,当用户选择一个特定的URL时,我调用api来访问它。 我的表格如下:

<div class="panel-body" ng-controller="NewsCtrl">
        <form class="form-inline" role="form">
            <div class="input-group">
                <div class="input-group-btn">
                    <button class="btn btn-info" type="button">{{loadButtonText}}</button>
                    <button class="btn btn-info dropdown-toggle" type="button" data-toggle="dropdown"><span class="caret"></span><span class="sr-only">Toggle-dropdown</span></button>
                    <ul class="dropdown-menu" role="menu">
                        <li ng-repeat="rss in RSSList">
                            <a href="#" ng-click="loadFeed(rss.url, $event);">{{rss.Title}}</a>
                        </li>
                    </ul>
                    <input type="text" class="form-control" autocomplete="off" placeholder="This is where your feed's url will appear" data-ng-model="url">
                </div>
            </div>
        </form>

我的路线/状态定义如下:

var myApp = angular.module('myApp',['ngSanitize','ui.router', 'myAppControllers', 'myAppFactories']);
myApp.config(['$stateProvider','$urlRouterProvider','$locationProvider', 
    function ($stateProvider,$urlRouterProvider,$locationProvider) {
    $stateProvider
        .state('home', {
            url:'/home',
            templateUrl: 'views/partials/partial-home.html'
        })
        .state('app', {
            url:'/api',
            templateUrl: 'views/partials/partial-news.html',
            controller: 'NewsCtrl' 
        });
    $urlRouterProvider.otherwise('/home');
    $locationProvider
    .html5Mode(true)
    .hashPrefix('!');
}]);

我的控制器如下:

var myApp = angular.module('myAppControllers',[]);
myApp.controller('NewsCtrl', ['$scope','MyService', function($scope, Feed){
    $scope.loadButtonText = 'Choose News Feed ';
    $scope.RSSList = [
        {Title: "CNN ",
        url: 'http://rss.cnn.com/rss/cnn_topstories.rss'},
        {Title: "CNBC Top News ",
        url: 'http://www.cnbc.com/id/100003114/device/rss/rss.html'},
    ];
        //Loading the news items
    $scope.loadFeed = function (url, e) {
        $scope.url= url;
        Feed.parseFeed(url).then(function (res) {
            $scope.loadButtonText=angular.element(e.target).text();
            $scope.feeds=res.data.query.results.item;
            });
    }
}]);

当我改为使用ui-router时,我的问题出现了,我知道我必须改变这一行

<a href="#" ng-click="loadFeed(rss.url, $event);">{{rss.Title}}</a>

使用ui-sref,但只是更改为<a ui-sref="loadFeed(rss.url, $event);">{{rss.Title}}</a> 仍然没有将我的网址上传到我的NewsCtrl控制器,任何线索?

奖金问题:当我在$ scope.loadButtonText之前插入console.log并打开开发者控制台时,它似乎上传了NewsCtrl 2次,为什么会这样?

1 个答案:

答案 0 :(得分:1)

ui-sref="app"

应该工作。

sref代表&#34;州&#34;引用,而不是熟悉的超链接&#34;参考。因此,它将查找使用.state();

定义的状态

或者,你可以选择一个好的老式网址,

href="#/api"

这会产生同样的效果。项目开发人员建议 sref