使用来自服务器的数据更新/刷新angularjs ng-repeat

时间:2015-05-15 11:36:18

标签: angularjs scope refresh ng-repeat

我尝试建立一个网站,喜欢一些图片。

我可以加载图片,但我想拥有一个喜欢图片的系统。

我的问题是我必须刷新页面以查看谁喜欢这张图片并增加计数。

这是我的控制器:

$scope.loadingpics = function (){
        $http.get('/api/photos')
        .success(function(awesomeThings) {

          console.log(awesomeThings);
          $scope.firstname = awesomeThings;
          })
          .error(function (err){
              $scope.errors.other = err.message;
            });
         };
$scope.upVote = function(index){
         $scope.vote = 1;
         var ref = index;
         var nom = index.url.substr(30, 40);
         var num = index.vote + 1;

         $http.put('api/photos/' + nom, {
          email: email, 
          vote: num
          })
         .success(function (data) {
              $scope.firstname = data;
              $scope.loadingpics();
          })          
          .error(function (err){
            $scope.errors.other = err.message;
          });
      };

这是我的观点:

  <li ng-repeat="image in firstname | orderBy: 'firstname'"  ng-show="isLoggedIn()" class="thumbnail" title="Image 1" on-last-repeat>                                   
<img  ng-src="../assets/images/Pouce.png" ng-click="upVote(image)" data-toggle="popover" data-content="And here's some amazing content. It's very engaging. Right?" data-placement="right" title="Popover title">
            </li>

这是我的架构:

var PhotoSchema = new Schema({
  url: String,
  firstname: String,
  email: [String],      
  info: String,
  vote: Number,
  active: Boolean
});

感谢您的帮助:D

2 个答案:

答案 0 :(得分:0)

不是只在架构中存储喜欢的数量,而是存储一个对象数组:

 var PhotoSchema = new Schema({
  url: String,
  firstname: String,
  email: [String],
  info: String,
  vote: [{
    date: Date,
    user: String
  }],
  active: Boolean
});

答案 1 :(得分:0)

我只是通过添加$ scope.watch和$ scope apply来改变我的upVote函数。

这是我的例子:

public class MyPrefs extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyPreferenceFragment frag = new MyPreferenceFragment();

        getFragmentManager().beginTransaction().replace(android.R.id.content, frag).commit();

        /* remove all of this
        Preference pref;
        pref = frag.findPreference("pref_gps_updates");
        pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                return true;
            }
        });
        */

    }


    public static class MyPreferenceFragment extends PreferenceFragment
            implements OnSharedPreferenceChangeListener {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }

        @Override
        public void onResume() {
            super.onResume();
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onPause() {
            super.onPause();
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

            if (key.equals("pref_gps_updates")) {
                //do something
            }
        }
    }
}